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


C# FieldBuilder.SetCustomAttribute方法代码示例

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


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

示例1: MyAttribute1

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

namespace MySample
{
   [AttributeUsage(AttributeTargets.All, AllowMultiple = false)]
   public class MyAttribute1 :Attribute
   {
      public string myCustomAttributeValue;
      public MyAttribute1(string myString)
      {
         myCustomAttributeValue = myString;
      }
   }
   [AttributeUsage(AttributeTargets.All, AllowMultiple = false)]
   public class MyAttribute2 :Attribute
   {
      public bool myCustomAttributeValue;
      public MyAttribute2(bool myBool)
      {
         myCustomAttributeValue = myBool;
      }
   }

   class FieldBuilder_Sample
   {
      private static Type CreateCallee(AppDomain currentDomain)
      {

         // Create a simple name for the assembly.
         AssemblyName myAssemblyName = new AssemblyName();
         myAssemblyName.Name = "EmittedAssembly";
         // Create the called dynamic assembly.
         AssemblyBuilder myAssemblyBuilder =
            currentDomain.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.RunAndSave);
         ModuleBuilder myModuleBuilder =
                  myAssemblyBuilder.DefineDynamicModule("EmittedModule","EmittedModule.mod");
         // Define a public class named 'CustomClass' in the assembly.
         TypeBuilder myTypeBuilder = myModuleBuilder.DefineType("CustomClass",
            TypeAttributes.Public);
         // Define a private String field named 'MyField' in the type.
         FieldBuilder myFieldBuilder =
            myTypeBuilder.DefineField("MyField", typeof(String), FieldAttributes.Public);
         Type myAttributeType1 = typeof(MyAttribute1);
         // Create a Constructorinfo object for attribute 'MyAttribute1'.
         ConstructorInfo myConstructorInfo = myAttributeType1.GetConstructor(
            new Type[1]{typeof(string)});
         // Create the CustomAttribute instance of attribute of type 'MyAttribute1'.
         CustomAttributeBuilder attributeBuilder =
                     new CustomAttributeBuilder( myConstructorInfo,new object[1]{"Test"});
         // Set the CustomAttribute 'MyAttribute1' to the Field.
         myFieldBuilder.SetCustomAttribute(attributeBuilder);

          Type myAttributeType2 = typeof(MyAttribute2);
         // Create a Constructorinfo object for attribute 'MyAttribute2'.
        ConstructorInfo myConstructorInfo2 = myAttributeType2.GetConstructor(
            new Type[1]{typeof(bool)});
         // Set the CustomAttribute 'MyAttribute2' to the Field.
         myFieldBuilder.SetCustomAttribute(myConstructorInfo2,new byte[]{01,00,01,00,00});

         // Create a method.
         MethodBuilder myMethodBuilder= myTypeBuilder.DefineMethod("MyMethod",
            MethodAttributes.Public,null,new Type[2]{typeof(string),typeof(int)});

         ILGenerator myILGenerator = myMethodBuilder.GetILGenerator();
         myILGenerator.Emit(OpCodes.Ldarg_0);
         myILGenerator.Emit(OpCodes.Ldarg_1);
         myILGenerator.Emit(OpCodes.Stfld, myFieldBuilder);
         myILGenerator.EmitWriteLine("Value of the Field is :");
         myILGenerator.EmitWriteLine(myFieldBuilder);
         myILGenerator.Emit(OpCodes.Ret);

         return myTypeBuilder.CreateType();
      }
      public static void Main()
      {
         try
         {
            Type myCustomClass = CreateCallee(Thread.GetDomain());
            // Construct an instance of a type.
            Object myObject = Activator.CreateInstance(myCustomClass);
            Console.WriteLine( "FieldBuilder Sample");
            // Find a method in this type and call it on this object.
            MethodInfo myMethodInfo = myCustomClass.GetMethod("MyMethod");
            myMethodInfo.Invoke(myObject, new object[2]{"Sample string",3});
            // Retrieve the values of Attributes applied to field and display to console.
            FieldInfo[] myFieldInfo = myCustomClass.GetFields();
            for(int i =0;i<myFieldInfo.Length;i++)
            {
               object[] attributes = myFieldInfo[i].GetCustomAttributes(true);
               for(int index=0; index < attributes.Length; index++)
               {
                  if(attributes[index] is MyAttribute1)
                  {
                     MyAttribute1 myCustomAttribute = (MyAttribute1)attributes[index];
                     Console.WriteLine("Attribute Value of (MyAttribute1): "
                                       + myCustomAttribute.myCustomAttributeValue);
                  }
                  if(attributes[index] is MyAttribute2)
                  {
                     MyAttribute2 myCustomAttribute = (MyAttribute2)attributes[index];
                     Console.WriteLine("Attribute Value of (MyAttribute2): "
                                       + myCustomAttribute.myCustomAttributeValue);
                  }
               }
            }
         }
         catch (Exception e)
         {
            Console.WriteLine("Exception Caught "+e.Message);
         }
      }
   }
}
开发者ID:.NET开发者,项目名称:System.Reflection.Emit,代码行数:117,代码来源:FieldBuilder.SetCustomAttribute


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