Click here to Skip to main content
15,884,472 members
Articles / Desktop Programming / WPF

Extending Castle DynamicProxy

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
8 May 2010CPOL7 min read 25.7K   183   12  
Shows how to extend the proxy generated by this framework by using Reflection.Emit.
using System;
using System.Reflection;
using System.Reflection.Emit;
using Castle.DynamicProxy;
using Castle.DynamicProxy.Generators;
using Castle.DynamicProxy.Generators.Emitters;
using Castle.DynamicProxy.Contributors;
using PresentationModelBase;

namespace Castle.Facilities.PresentationModelIntegration
{
    public class EntityViewEncapsulationPropertyTypeContributor : ITypeContributor
    {
        public EntityViewEncapsulationPropertyTypeContributor(string encapsulatedPropertyName, Type entityType, Type propertyReturnType, Type entityViewPresentationModelType)
        {
            _encapsulatedPropertyName = encapsulatedPropertyName;
            _entityType = entityType;           
            _propertyReturnType = propertyReturnType;
            _entityViewPresentationModelType = entityViewPresentationModelType;
        }

        private string _encapsulatedPropertyName;
        private Type _entityType;        
        private Type _propertyReturnType;
        private Type _entityViewPresentationModelType;       

        public void CollectElementsToProxy(IProxyGenerationHook hook, MetaType model)
        {
            //
        }

        public void Generate(ClassEmitter @class, ProxyGenerationOptions options)
        {
            string propertyName = _entityType.Name + _encapsulatedPropertyName;
            GenerateEncapsulationProperty(@class.TypeBuilder, propertyName);
        }

        protected virtual void GenerateEncapsulationProperty(TypeBuilder typeBuilder, string propertyName) 
        {
            var constructedPresentationModelInterface = typeof(IEntityViewPresentationModel<>).MakeGenericType(_entityType);

            var propertyBuilder = typeBuilder.DefineProperty(
                propertyName,
                PropertyAttributes.None,
                _propertyReturnType,
                Type.EmptyTypes);
            
            var getterBuilder = typeBuilder.DefineMethod(
                string.Format("get_{0}", propertyName),
                MethodAttributes.SpecialName | MethodAttributes.HideBySig | MethodAttributes.Public,
                _propertyReturnType,
                Type.EmptyTypes);
            var getterGenerator = getterBuilder.GetILGenerator();

            getterGenerator.Emit(OpCodes.Ldarg_0);
            getterGenerator.Emit(OpCodes.Call, constructedPresentationModelInterface.GetProperty("Entity").GetGetMethod());
            getterGenerator.Emit(OpCodes.Call, _entityType.GetProperty(_encapsulatedPropertyName).GetGetMethod());
            getterGenerator.Emit(OpCodes.Ret);

            var setterBuilder = typeBuilder.DefineMethod(
                string.Format("set_{0}", propertyName),
                MethodAttributes.SpecialName | MethodAttributes.HideBySig | MethodAttributes.Public,
                null,
                new Type[] { _propertyReturnType });
            var setterGenerator = setterBuilder.GetILGenerator();

            Label returnStatement = setterGenerator.DefineLabel();             
            setterGenerator.Emit(OpCodes.Ldarg_1);
            setterGenerator.Emit(OpCodes.Ldarg_0);
            setterGenerator.Emit(OpCodes.Call, constructedPresentationModelInterface.GetProperty("Entity").GetGetMethod());
            setterGenerator.Emit(OpCodes.Call, _entityType.GetProperty(_encapsulatedPropertyName).GetGetMethod());
            setterGenerator.Emit(OpCodes.Ceq);
            setterGenerator.Emit(OpCodes.Brtrue, returnStatement);            
            setterGenerator.Emit(OpCodes.Ldarg_0);           
            setterGenerator.Emit(OpCodes.Callvirt, constructedPresentationModelInterface.GetProperty("Entity").GetGetMethod());
            setterGenerator.Emit(OpCodes.Ldarg_1);
            setterGenerator.Emit(OpCodes.Call, _entityType.GetProperty(_encapsulatedPropertyName).GetSetMethod());
            setterGenerator.Emit(OpCodes.Ldarg_0);
            setterGenerator.Emit(OpCodes.Ldstr, propertyName);
            setterGenerator.Emit(OpCodes.Callvirt, _entityViewPresentationModelType.GetMethod("OnPropertyChanged", BindingFlags.NonPublic | BindingFlags.Instance));
            setterGenerator.MarkLabel(returnStatement);
            setterGenerator.Emit(OpCodes.Ret);


            propertyBuilder.SetGetMethod(getterBuilder);
            propertyBuilder.SetSetMethod(setterBuilder);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Brazil Brazil
Software developer specialized in the .NET framework

Comments and Discussions