本文整理汇总了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());
}
}