当前位置: 首页>>代码示例>>C#>>正文


C# ConstructorBuilder.SetCustomAttribute方法代码示例

本文整理汇总了C#中System.Reflection.Emit.ConstructorBuilder.SetCustomAttribute方法的典型用法代码示例。如果您正苦于以下问题:C# ConstructorBuilder.SetCustomAttribute方法的具体用法?C# ConstructorBuilder.SetCustomAttribute怎么用?C# ConstructorBuilder.SetCustomAttribute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Reflection.Emit.ConstructorBuilder的用法示例。


在下文中一共展示了ConstructorBuilder.SetCustomAttribute方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: MyAttribute

//引入命名空间
using System;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;

[AttributeUsage(AttributeTargets.All, AllowMultiple = false)]
public class MyAttribute : Attribute
{
   public String myString;
   public int myInteger;
   public MyAttribute(String myString, int myInteger)
   {
      this.myString = myString;
      this.myInteger = myInteger;
   }
}

public class MyConstructorBuilder
{
   public static void Main()
   {
      Type myHelloworld = MyCreateCallee(Thread.GetDomain());
      ConstructorInfo myConstructor = myHelloworld.GetConstructor(new Type[]{typeof(String)});
      object[] myAttributes1 = myConstructor.GetCustomAttributes(true);
      Console.WriteLine("MyAttribute custom attribute contains  ");
      for(int index=0; index < myAttributes1.Length; index++)
      {
         if(myAttributes1[index] is MyAttribute)
         {
            Console.WriteLine("The value of myString is : " 
                                       + ((MyAttribute)myAttributes1[index]).myString);
            Console.WriteLine("The value of myInteger is : " 
                                       + ((MyAttribute)myAttributes1[index]).myInteger);
         }
      }
   }

   private static Type MyCreateCallee(AppDomain domain)
   {
      AssemblyName myAssemblyName = new AssemblyName();
      myAssemblyName.Name = "EmittedAssembly";
      // Define a dynamic assembly in the current application domain.
      AssemblyBuilder myAssembly =
                  domain.DefineDynamicAssembly(myAssemblyName,AssemblyBuilderAccess.Run);
      // Define a dynamic module in this assembly.
      ModuleBuilder myModuleBuilder = myAssembly.DefineDynamicModule("EmittedModule");
       // Construct a 'TypeBuilder' given the name and attributes.
      TypeBuilder myTypeBuilder = myModuleBuilder.DefineType("HelloWorld",
         TypeAttributes.Public);
      // Define a constructor of the dynamic class.
      ConstructorBuilder myConstructor = myTypeBuilder.DefineConstructor(
               MethodAttributes.Public, CallingConventions.Standard, new Type[]{typeof(String)});
      ILGenerator myILGenerator = myConstructor.GetILGenerator();
      myILGenerator.Emit(OpCodes.Ldstr, "Constructor is invoked");
      myILGenerator.Emit(OpCodes.Ldarg_1);
      MethodInfo myMethodInfo =
                     typeof(Console).GetMethod("WriteLine",new Type[]{typeof(string)});
      myILGenerator.Emit(OpCodes.Call, myMethodInfo);
      myILGenerator.Emit(OpCodes.Ret);
      Type myType = typeof(MyAttribute);
      ConstructorInfo myConstructorInfo = myType.GetConstructor(new Type[2]{typeof(String), typeof(int)});
      CustomAttributeBuilder attributeBuilder =
         new CustomAttributeBuilder(myConstructorInfo, new object[2]{"Hello", 2});
      try
      {
         myConstructor.SetCustomAttribute(attributeBuilder);
      }
      catch(ArgumentNullException ex)
      {
         Console.WriteLine("The following exception has occurred : "+ex.Message);
      }
      catch(Exception ex)
      {
         Console.WriteLine("The following exception has occurred : "+ex.Message);
      }
      return myTypeBuilder.CreateType();
   }
}
开发者ID:.NET开发者,项目名称:System.Reflection.Emit,代码行数:79,代码来源:ConstructorBuilder.SetCustomAttribute

示例2: MyAttribute

//引入命名空间
using System;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;

[AttributeUsage(AttributeTargets.All, AllowMultiple = false)]
public class MyAttribute : Attribute
{
   public bool myBoolean;

   public MyAttribute(bool myBoolean)
   {
      this.myBoolean = myBoolean;
   }
}

public class MyConstructorBuilder
{
   public static void Main()
   {
      Type myHelloworld = MyCreateCallee(Thread.GetDomain());
      ConstructorInfo myConstructor = myHelloworld.GetConstructor(new Type[]{typeof(String)});
      object[] myAttributes1 = myConstructor.GetCustomAttributes(true);
      Console.WriteLine("MyAttribute custom attribute contains  ");
      for(int index=0; index < myAttributes1.Length; index++)
      {
         if(myAttributes1[index] is MyAttribute)
         {
            Console.WriteLine("myBoolean : " + ((MyAttribute)myAttributes1[index]).myBoolean);
         }
      }
   }

   private static Type MyCreateCallee(AppDomain domain)
   {
      AssemblyName myAssemblyName = new AssemblyName();
      myAssemblyName.Name = "EmittedAssembly";
      // Define a dynamic assembly in the current application domain.
      AssemblyBuilder myAssembly =
                  domain.DefineDynamicAssembly(myAssemblyName,AssemblyBuilderAccess.Run);
      // Define a dynamic module in this assembly.
      ModuleBuilder myModuleBuilder = myAssembly.DefineDynamicModule("EmittedModule");
      // Construct a 'TypeBuilder' given the name and attributes.
      TypeBuilder myTypeBuilder = myModuleBuilder.DefineType("HelloWorld",
         TypeAttributes.Public);
      // Define a constructor of the dynamic class.
      ConstructorBuilder myConstructor = myTypeBuilder.DefineConstructor(
               MethodAttributes.Public, CallingConventions.Standard, new Type[]{typeof(String)});
      ILGenerator myILGenerator = myConstructor.GetILGenerator();
      myILGenerator.Emit(OpCodes.Ldstr, "Constructor is invoked");
      myILGenerator.Emit(OpCodes.Ldarg_1);
      MethodInfo myMethodInfo =
                     typeof(Console).GetMethod("WriteLine",new Type[]{typeof(string)});
      myILGenerator.Emit(OpCodes.Call, myMethodInfo);
      myILGenerator.Emit(OpCodes.Ret);
      Type myType = typeof(MyAttribute);
      ConstructorInfo myConstructorInfo = myType.GetConstructor(new Type[]{typeof(bool)});
      try
      {
         myConstructor.SetCustomAttribute(myConstructorInfo, new byte[]{01,00,01});
      }
      catch(ArgumentNullException ex)
      {
         Console.WriteLine("The following exception has occurred : "+ex.Message);
      }
      catch(Exception ex)
      {
         Console.WriteLine("The following exception has occurred : "+ex.Message);
      }
      return myTypeBuilder.CreateType();
   }
}
开发者ID:.NET开发者,项目名称:System.Reflection.Emit,代码行数:73,代码来源:ConstructorBuilder.SetCustomAttribute


注:本文中的System.Reflection.Emit.ConstructorBuilder.SetCustomAttribute方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。