本文整理匯總了C#中System.Reflection.Emit.AssemblyBuilder.SetCustomAttribute方法的典型用法代碼示例。如果您正苦於以下問題:C# AssemblyBuilder.SetCustomAttribute方法的具體用法?C# AssemblyBuilder.SetCustomAttribute怎麽用?C# AssemblyBuilder.SetCustomAttribute使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Reflection.Emit.AssemblyBuilder
的用法示例。
在下文中一共展示了AssemblyBuilder.SetCustomAttribute方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: MyAttribute
[AttributeUsage(AttributeTargets.All, AllowMultiple = false)]
public class MyAttribute : Attribute
{
public String s;
public int x;
public MyAttribute(String s, int x)
{
this.s = s;
this.x = x;
}
}
class MyApplication
{
public static void Main()
{
Type customAttribute = CreateCallee(Thread.GetDomain());
object[] attributes = customAttribute.Assembly.GetCustomAttributes(true);
Console.WriteLine("MyAttribute custom attribute contains : ");
for(int index=0; index < attributes.Length; index++)
{
if(attributes[index] is MyAttribute)
{
Console.WriteLine("s : " + ((MyAttribute)attributes[index]).s);
Console.WriteLine("x : " + ((MyAttribute)attributes[index]).x);
break;
}
}
}
private static Type CreateCallee(AppDomain domain)
{
AssemblyName myAssemblyName = new AssemblyName();
myAssemblyName.Name = "EmittedAssembly";
AssemblyBuilder myAssembly = domain.DefineDynamicAssembly(myAssemblyName,
AssemblyBuilderAccess.Run);
Type myType = typeof(MyAttribute);
ConstructorInfo infoConstructor = myType.GetConstructor(new Type[2]{typeof(String), typeof(int)});
CustomAttributeBuilder attributeBuilder =
new CustomAttributeBuilder(infoConstructor, new object[2]{"Hello", 2});
myAssembly.SetCustomAttribute(attributeBuilder);
ModuleBuilder myModule = myAssembly.DefineDynamicModule("EmittedModule");
// Define a public class named "HelloWorld" in the assembly.
TypeBuilder helloWorldClass = myModule.DefineType("HelloWorld", TypeAttributes.Public);
return(helloWorldClass.CreateType());
}
}
示例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 s;
public MyAttribute(bool s)
{
this.s = s;
}
}
class MyApplication
{
public static void Main()
{
Type customAttribute = CreateCallee(Thread.GetDomain());
object[] attributes = customAttribute.Assembly.GetCustomAttributes(true);
Console.WriteLine("MyAttribute custom attribute contains : ");
for(int index=0; index < attributes.Length; index++)
{
if(attributes[index] is MyAttribute)
{
Console.WriteLine("s : " + ((MyAttribute)attributes[index]).s);
break;
}
}
}
private static Type CreateCallee(AppDomain domain)
{
AssemblyName myAssemblyName = new AssemblyName();
myAssemblyName.Name = "EmittedAssembly";
AssemblyBuilder myAssembly = domain.DefineDynamicAssembly(myAssemblyName,
AssemblyBuilderAccess.Run);
Type myType = typeof(MyAttribute);
ConstructorInfo infoConstructor = myType.GetConstructor(new Type[]{typeof(bool)});
myAssembly.SetCustomAttribute(infoConstructor, new byte[]{01,00,01});
ModuleBuilder myModule = myAssembly.DefineDynamicModule("EmittedModule");
// Define a public class named "HelloWorld" in the assembly.
TypeBuilder helloWorldClass = myModule.DefineType("HelloWorld", TypeAttributes.Public);
return(helloWorldClass.CreateType());
}
}