- castleextensionsdemo.zip
- App.Configuration
- App.Core
- App.Data
- Castle.ExtensibleDynamicProxy
- Castle.Facilities.PresentationModelIntegration
- CastleExtensionsDemo.sln
- libs
- AttachedCommandBehavior.dll
- Castle.Core.dll
- Castle.DynamicProxy2.dll
- Castle.MicroKernel.dll
- Castle.Windsor.dll
- PresentationModelBase
- WPFUI
|
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.