熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

AspectSharp 中的代理對象全接觸

2022-06-13   來源: .NET編程 

  代理對象是AOP動態截入的關鍵部分 下面來分析一下代理對象是如何定義的
  
   CreateProxyType
  internal static Type CreateProxyType(Type[] interfaces IMixin[] mixins) {
  //
  AssemblyName assemblyName = new AssemblyName();
  assemblyNameName = DynamicAssemblyProxyGen;
  
  AssemblyBuilder assemblyBuilder = AppDomainCurrentDomainDefineDynamicAssembly(
  assemblyName AssemblyBuilderAccessRun);
  // 以指定名稱和訪問模式定義動態程序集 這裡以運行模式建立程序集
  
  ModuleBuilder moduleBuilder = assemblyBuilderDefineDynamicModule( assemblyNameName true );
  // 在動態程序集中定義動態模塊 第二個參數發出符號信息
  
  TypeBuilder typeBuilder = moduleBuilderDefineType( ProxyType
  TypeAttributesPublic|TypeAttributesClass null MergeTypes(interfaces mixins) );
  // 定義一個類類型 此類為public 且實現所有的interfaces和mixins接口
  // MergeTypes用於將interfaces和mixins合並為一個Type[]
  
  FieldBuilder handlerField = GenerateField( typeBuilder handlerFieldName typeof(IInvocationHandler) );
  // 產生一個IInvocationHandler字段 等同於 public IInvocationHandler handler;
  
  FieldBuilder mixinHandlerField = GenerateField( typeBuilder mixinHandlerFieldName
  typeof(IMixinInvocationHandler) );
  // 產生一個IMixinInvocation字段 等同於 public IMixinInvocationHandler mixinHandler;
  
  FieldBuilder mixinsField = GenerateField( typeBuilder mixins typeof(IMixin[]) );
  // 產生一個IMixin[]字段 等同於 public IMixin[] mixins;
  
  ConstructorBuilder constr = GenerateConstructor( typeBuilder handlerField mixinsField mixinHandlerField );
  // 產生一個構造方法
  
  GenerateInterfaceImplementation( typeBuilder interfaces handlerField );
  // 產生接口的實現部分
  
  GenerateMixinImplementation( typeBuilder mixins mixinHandlerField mixinsField);
  // 產生混合器接口的實現部分
  }
  
   GenerateConstructor
  private static ConstructorBuilder GenerateConstructor( TypeBuilder typeBuilder
  FieldBuilder handlerField FieldBuilder mixinsField FieldBuilder mixinHandlerField ) {
  ConstructorBuilder consBuilder = typeBuilderDefineConstructor(
  MethodAttributesPublic
  CallingConventionsStandard
  new Type[] { typeof(IInvocationHandler) typeof(IMixinInvocationHandler) typeof(IMixin[]) } );
  
  // 定義一個構造方法 指定方法修飾為Public 調用約定為Standard
  // 等同於 public ProxyType( IInvocationHandler handler IMixinInvocationHandler mixinHandler IMixin[] mixins );
  
  ILGenerator ilGenerator = consBuilderGetILGenerator();
  // 取得IL產生器 用於生成IL代碼
  
  ilGeneratorEmit(OpCodesLdarg_);
  ilGeneratorEmit(OpCodesCall typeof(Object)GetConstructor(new Type[]));
  // 調用Object類型的默認構造函數 等同於 public ProxyType() : base()
  
  // handler
  ilGeneratorEmit(OpCodesLdarg_);
  ilGeneratorEmit(OpCodesLdarg_);
  ilGeneratorEmit(OpCodesStfld handlerField);
  // 對handlerField字段進行賦值 等同於 thishandler = handler;
  
  // mixinHandler
  ilGeneratorEmit(OpCodesLdarg_);
  ilGeneratorEmit(OpCodesLdarg_);
  ilGeneratorEmit(OpCodesStfld mixinHandlerField);
  // 對mixinHandlerField字段進行賦值 等同於 thismixinHandler = mixinHandler;
  
  // mixins
  ilGeneratorEmit(OpCodesLdarg_);
  ilGeneratorEmit(OpCodesLdarg_);
  ilGeneratorEmit(OpCodesStfld mixinsField);
  // 對mixinsField字段進行賦值 等同於 thismixins = mixins
  
  ilGeneratorEmit(OpCodesRet);
  
  return consBuilder;
  }
  
  代理對象的構造方法類似如下:
  
  public Class ProxyType : 對象接口 混合器接口 {
  private IInvocationHandler handler;
  private IMixinvocationHandler mixinHandler;
  private IMixin[] mixins;
  
  public public ProxyType( IInvocationHandler handler
  IMixinInvocationHandler mixinHandler IMixin[] mixins );  {
  thishandler = handler;
  thismixinHandler = mixinHandler;
  thismixins = mixins;
  }
  
  //
  }
  
   產生接口的實現部分
  
  static void GenerateInterfaceImplementation( TypeBuilder typeBuilder
  Type[] interfaces FieldBuilder handlerField ) {
  foreach(Type inter in interfaces) {
  GenerateInterfaceImplementation( typeBuilder inter handlerField null );
  }
  // 遍歷所有接口
  }
  
  private static void GenerateMixinImplementation( TypeBuilder typeBuilder
  IMixin[] mixins FieldBuilder mixinHandlerField FieldBuilder mixinsField) {
  if (mixins == null) return;
  
  for(int i = ; i < mixins.Length; i++) {
  Type[] interfaces = mixins[i].GetType().GetInterfaces();
  
  foreach(Type inter in interfaces) {
  GenerateInterfaceImplementation( typeBuilder, inter, mixinHandlerField, mixinsField, i );
  }
  }
  // 遍歷所有的混合器接口
  }
  
  static void GenerateInterfaceImplementation( TypeBuilder typeBuilder,
  Type inter, FieldBuilder handlerField, FieldBuilder mixinsField, int mixinPosition) {
  // ...
  
  Type[] baseInterfaces = inter.FindInterfaces( new TypeFilter( NoFilterImpl ), inter );
  GenerateInterfaceImplementation( typeBuilder, baseInterfaces, handlerField);
  // 這是一個遞歸過程, 用於遍歷所有的接口.
  
  PropertyInfo[] properties = inter.GetProperties();
  PropertyBuilder[] propertiesBuilder = new PropertyBuilder[properties.Length];
  for(int i=0; i < properties.Length; i++) {
  GeneratePropertyImplementation( typeBuilder, properties[i], ref propertiesBuilder[i] );
  }
  // 產生屬性的實現.
  
  MethodInfo[] methods = inter.GetMethods();
  foreach(MethodInfo method in methods) {
  GenerateMethodImplementation( typeBuilder, method, propertiesBuilder, inter,
  handlerField, mixinsField, mixinPosition );
  }
  // 產生方法的實現.
  }
  
  // 屬性的實現.
  private static void GeneratePropertyImplementation( TypeBuilder typeBuilder,
  PropertyInfo property, ref PropertyBuilder propertyBuilder ) {
  propertyBuilder = typeBuilder.DefineProperty(
  property.Name, property.Attributes, property.PropertyType, null);
  // 通過調用DefindProperty方法來產生屬性的實現部分.
  }
  
  // 方法的實現.
  private static void GenerateMethodImplementation( TypeBuilder typeBuilder,
  MethodInfo method, PropertyBuilder[] properties,  Type inter,
  FieldBuilder handlerField, FieldBuilder mixinsField, int mixinPosition ) {
  ParameterInfo[] parameterInfo = method.GetParameters();
  // 取得參數信息.
  
  System.Type[] parameters = new System.Type[parameterInfo.Length];
  for (int i=0; i  parameters[i] = parameterInfo[i].ParameterType;
  }
  // 取得參數的類型.
  
  MethodAttributes atts = MethodAttributes.Public|MethodAttributes.Virtual;
  if ( method.Name.StartsWith("set_") || method.Name.StartsWith("get_") ) {
  atts = MethodAttributes.Public|MethodAttributes.SpecialName|MethodAttributes.Virtual;
  }
  // 指定方法的修飾.
  
  MethodBuilder methodBuilder = typeBuilder.DefineMethod( method.Name, atts,
  CallingConventions.Standard, method.ReturnType, parameters );
  // 定義方法.
  
  if ( method.Name.StartsWith("set_") || method.Name.StartsWith("get_") ) {
  foreach( PropertyBuilder property in properties ) {
  if (property == null) {
  break;
  }
  
  if (!property.Name.Equals( method.Name.Substring(4) )) {
  continue;
  } From:http://tw.wingwit.com/Article/program/net/201311/13593.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.