本文整理汇总了C#中System.Reflection.Emit.CustomAttributeBuilder类的典型用法代码示例。如果您正苦于以下问题:C# CustomAttributeBuilder类的具体用法?C# CustomAttributeBuilder怎么用?C# CustomAttributeBuilder使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
CustomAttributeBuilder类属于System.Reflection.Emit命名空间,在下文中一共展示了CustomAttributeBuilder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DynamicAssembly
public DynamicAssembly()
{
int assemblyNumber = Interlocked.Increment(ref _assemblyCount);
_assemblyName = new AssemblyName {
Name = String.Format("Quokka.DynamicAssembly.N{0}", assemblyNumber)
};
string moduleName = AssemblyName.Name;
_dynamicClassNamespace = AssemblyName.Name;
if (CreateFiles)
{
_assemblyBuilder = Thread.GetDomain().DefineDynamicAssembly(
AssemblyName, AssemblyBuilderAccess.RunAndSave);
// Add a debuggable attribute to the assembly saying to disable optimizations
// See http://blogs.msdn.com/rmbyers/archive/2005/06/26/432922.aspx
Type daType = typeof (DebuggableAttribute);
ConstructorInfo daCtor = daType.GetConstructor(new[] {typeof (bool), typeof (bool)});
var daBuilder = new CustomAttributeBuilder(daCtor, new object[] {true, true});
_assemblyBuilder.SetCustomAttribute(daBuilder);
_moduleBuilder = _assemblyBuilder.DefineDynamicModule(moduleName);
_canSave = true;
}
else
{
_assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess.Run);
_moduleBuilder = _assemblyBuilder.DefineDynamicModule(moduleName);
}
}
示例2: CustAttr
public CustAttr(CustomAttributeBuilder customBuilder)
{
if (customBuilder == null)
throw new ArgumentNullException("customBuilder");
m_customBuilder = customBuilder;
}
示例3: GenerateAnonymousType
private Type GenerateAnonymousType(ModuleBuilder dynamicTypeModule)
{
var dynamicType = dynamicTypeModule.DefineType(ClassName, TypeAttributes.BeforeFieldInit | TypeAttributes.Sealed);
var builderArray = dynamicType.DefineGenericParameters(GenericClassParameterNames);
var fields = new List<FieldBuilder>();
for (var i = 0; i < FieldNames.Length; i++)
{
var item = dynamicType.DefineField(FieldNames[i], builderArray[i], FieldAttributes.InitOnly | FieldAttributes.Private);
var type = typeof(DebuggerBrowsableAttribute);
var customBuilder = new CustomAttributeBuilder(type.GetConstructor(new Type[] { typeof(DebuggerBrowsableState) }), new object[] { DebuggerBrowsableState.Never });
item.SetCustomAttribute(customBuilder);
fields.Add(item);
}
var list2 = new List<PropertyBuilder>();
for (var j = 0; j < PropertyNames.Length; j++)
{
var builder4 = GenerateProperty(dynamicType, PropertyNames[j], fields[j]);
// var type = typeof(JsonPropertyAttribute);
// var customBuilder2 = new CustomAttributeBuilder(type.GetConstructor(new Type[0]), new object[0]);
// builder4.SetCustomAttribute(customBuilder2);
list2.Add(builder4);
}
GenerateClassAttributes(dynamicType, PropertyNames);
GenerateConstructor(dynamicType, PropertyNames, fields);
GenerateEqualsMethod(dynamicType, fields.ToArray());
GenerateGetHashCodeMethod(dynamicType, fields.ToArray());
GenerateToStringMethod(dynamicType, PropertyNames, fields.ToArray());
return dynamicType.CreateType().MakeGenericType(PropertyTypes);
}
示例4: RegexDynamicModule
protected RegexDynamicModule(int moduleNum, AssemblyName an, CustomAttributeBuilder[] attribs, String resourceFile) {
new ReflectionPermission(PermissionState.Unrestricted).Assert();
try {
if (an == null) {
an = new AssemblyName();
an.Name = "RegexAssembly" + AppDomain.CurrentDomain.GetHashCode().ToString() + "_" + moduleNum.ToString();
_assembly = AppDomain.CurrentDomain.DefineDynamicAssembly(an, AssemblyBuilderAccess.Run);
}
else {
_assembly = AppDomain.CurrentDomain.DefineDynamicAssembly(an, AssemblyBuilderAccess.RunAndSave);
}
_module = _assembly.DefineDynamicModule(an.Name + ".dll");
if (attribs != null) {
for (int i=0; i<attribs.Length; i++) {
_assembly.SetCustomAttribute(attribs[i]);
}
}
if (resourceFile != null) {
// unmanaged resources are not supported
throw new ArgumentOutOfRangeException("resourceFile");
}
}
finally {
CodeAccessPermission.RevertAssert();
}
}
示例5: RegexTypeCompiler
internal RegexTypeCompiler(AssemblyName an, CustomAttributeBuilder[] attribs, string resourceFile)
{
new ReflectionPermission(PermissionState.Unrestricted).Assert();
try
{
List<CustomAttributeBuilder> assemblyAttributes = new List<CustomAttributeBuilder>();
CustomAttributeBuilder item = new CustomAttributeBuilder(typeof(SecurityTransparentAttribute).GetConstructor(Type.EmptyTypes), new object[0]);
assemblyAttributes.Add(item);
CustomAttributeBuilder builder2 = new CustomAttributeBuilder(typeof(SecurityRulesAttribute).GetConstructor(new Type[] { typeof(SecurityRuleSet) }), new object[] { SecurityRuleSet.Level2 });
assemblyAttributes.Add(builder2);
this._assembly = AppDomain.CurrentDomain.DefineDynamicAssembly(an, AssemblyBuilderAccess.RunAndSave, assemblyAttributes);
this._module = this._assembly.DefineDynamicModule(an.Name + ".dll");
if (attribs != null)
{
for (int i = 0; i < attribs.Length; i++)
{
this._assembly.SetCustomAttribute(attribs[i]);
}
}
if (resourceFile != null)
{
this._assembly.DefineUnmanagedResource(resourceFile);
}
}
finally
{
CodeAccessPermission.RevertAssert();
}
}
示例6: Main
public static void Main()
{
AssemblyName assemblyName = new AssemblyName();
assemblyName.Name = "DynamicllyGeneratedAssembly";
AssemblyBuilder assemblyBuilder = System.Threading.Thread.GetDomain().DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndSave);
ConstructorInfo daCtor = typeof(DebuggableAttribute).GetConstructor(new Type[] { typeof(DebuggableAttribute.DebuggingModes) });
CustomAttributeBuilder daBuilder = new CustomAttributeBuilder(daCtor, new object[] { DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.Default });
assemblyBuilder.SetCustomAttribute(daBuilder);
ModuleBuilder module = assemblyBuilder.DefineDynamicModule("DynamicllyGeneratedModule.exe", true);
ISymbolDocumentWriter doc = module.DefineDocument(@"Source.txt", Guid.Empty, Guid.Empty, Guid.Empty);
TypeBuilder typeBuilder = module.DefineType("DynamicllyGeneratedType", TypeAttributes.Public | TypeAttributes.Class);
MethodBuilder methodbuilder = typeBuilder.DefineMethod("Main", MethodAttributes.HideBySig | MethodAttributes.Static | MethodAttributes.Public, typeof(void), new Type[] { typeof(string[]) });
ILGenerator ilGenerator = methodbuilder.GetILGenerator();
ilGenerator.MarkSequencePoint(doc, 1, 1, 1, 100);
ilGenerator.Emit(OpCodes.Ldstr, "Hello world!");
MethodInfo infoWriteLine = typeof(System.Console).GetMethod("WriteLine", new Type[] { typeof(string) });
ilGenerator.EmitCall(OpCodes.Call, infoWriteLine, null);
ilGenerator.MarkSequencePoint(doc, 2, 1, 2, 100);
ilGenerator.Emit(OpCodes.Ret);
Type helloWorldType = typeBuilder.CreateType();
System.Diagnostics.Debugger.Break();
helloWorldType.GetMethod("Main").Invoke(null, new string[] { null });
}
示例7: Create
private static TypeBuilder Create(string tableName, List<FieldItem> fields)
{
//在模块中创建类型
TypeBuilder typeBuilder = GetTypeBuilder(tableName);
//设置类型的父类
//typeBuilder.SetParent(typeof(Core));
//创建构造函数
ConstructorBuilder consBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, Type.EmptyTypes);
ILGenerator ctorIL = consBuilder.GetILGenerator();
ctorIL.Emit(OpCodes.Ret);
ConstructorInfo classCtorInfo = typeof(DMTableAttribute).GetConstructor(new Type[] { typeof(string), typeof(string), typeof(bool) });
CustomAttributeBuilder attr = new CustomAttributeBuilder(
classCtorInfo,
new object[] { tableName, "ContentID", true });
typeBuilder.SetCustomAttribute(attr);
foreach (var pi in fields)
{
CreateProperty(typeBuilder, pi.Name, pi.PropertyType);
}
return typeBuilder;
}
示例8: AddAttribute
public static void AddAttribute(this AssemblyBuilder assemblyBuilder, Type attributeType)
{
ConstructorInfo attributeCtor = attributeType.GetConstructor(new Type[] { });
CustomAttributeBuilder attributeBuilder =
new CustomAttributeBuilder(attributeCtor, new object[] { });
assemblyBuilder.SetCustomAttribute(attributeBuilder);
}
示例9: Create_class
public void Create_class()
{
ILGenerator ilGenerator;
AssemblyBuilder asmBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(
new AssemblyName("Scenarios"), AssemblyBuilderAccess.RunAndSave);
ModuleBuilder moduleBuilder = asmBuilder.DefineDynamicModule("Scenarios", "Scenarios.dll");
var typeBuilder = moduleBuilder.DefineType("Scenario_Checkout", TypeAttributes.Class | TypeAttributes.Public);
var attrBuilder = new CustomAttributeBuilder(typeof(TestFixtureAttribute).GetConstructor(new Type[]{ }), new Object[]{ });
typeBuilder.SetCustomAttribute(attrBuilder);
var method = typeBuilder.DefineMethod("ShouldGetLatestChangeset", MethodAttributes.Public);
attrBuilder = new CustomAttributeBuilder(typeof(TestAttribute).GetConstructor(new Type[]{ }), new Object[]{ });
method.SetCustomAttribute(attrBuilder);
ilGenerator = method.GetILGenerator();
ilGenerator.Emit(OpCodes.Ldstr, "Hello world");
ilGenerator.Emit(OpCodes.Call,
typeof(Console).GetMethod("WriteLine",
new Type[] { typeof(string)}));
//ilGenerator.Emit(OpCodes.Ldc_I4_0);
ilGenerator.Emit(OpCodes.Ret);
/*
var thisType = typeof(CreateCodeAtRuntimeTests);
var methodBody = thisType.GetMethod("Haldis").GetMethodBody().GetILAsByteArray();
method.CreateMethodBody(methodBody, methodBody.Length);
*/
typeBuilder.CreateType();
asmBuilder.Save("Scenarios.dll");
}
示例10: PosTest1
public void PosTest1()
{
TypeBuilder typebuilder = GetTypeBuilder();
ConstructorInfo constructorinfo = typeof(ClassCreator).GetConstructor(new Type[] { typeof(string) });
CustomAttributeBuilder cuatbu = new CustomAttributeBuilder(constructorinfo, new object[] { "hello" });
typebuilder.SetCustomAttribute(cuatbu);
}
示例11: NotImplementedException
public static void CompileToAssembly
(RegexCompilationInfo[] regexes, AssemblyName aname,
CustomAttributeBuilder[] attribs, string resourceFile)
{
throw new NotImplementedException ();
// TODO : Make use of attribs and resourceFile parameters
/*
AssemblyBuilder asmBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly (aname, AssemblyBuilderAccess.RunAndSave);
ModuleBuilder modBuilder = asmBuilder.DefineDynamicModule("InnerRegexModule",aname.Name);
Parser psr = new Parser ();
System.Console.WriteLine("CompileToAssembly");
for(int i=0; i < regexes.Length; i++)
{
System.Console.WriteLine("Compiling expression :" + regexes[i].Pattern);
RegularExpression re = psr.ParseRegularExpression (regexes[i].Pattern, regexes[i].Options);
// compile
CILCompiler cmp = new CILCompiler (modBuilder, i);
bool reverse = (regexes[i].Options & RegexOptions.RightToLeft) !=0;
re.Compile (cmp, reverse);
cmp.Close();
}
// Define a runtime class with specified name and attributes.
TypeBuilder builder = modBuilder.DefineType("ITest");
builder.CreateType();
asmBuilder.Save(aname.Name);
*/
}
示例12: ExecuteTask
// In here, we're defining a class, foo_Task, derived from our DefinedTask class. This allows us to write the bulk of the code
// in real C#, rather than attempting to build it here.
// This means that we have Task <- DefinedTask <- foo_Task.
// We also need a module and an assembly to hold the class, so we do that, too.
protected override void ExecuteTask()
{
AssemblyName assemblyName = new AssemblyName();
assemblyName.Name = _taskName + "_Assembly";
AssemblyBuilder assemblyBuilder =
AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule(_taskName + "_Module");
// Now we've got an assembly and a module for the task to live in, we can define the actual task class.
TypeBuilder typeBuilder = moduleBuilder.DefineType(_taskName + "_Task", TypeAttributes.Public);
typeBuilder.SetParent(typeof(DefinedTask));
// It needs a [TaskName] attribute.
ConstructorInfo taskNameAttributeConstructor =
typeof(TaskNameAttribute).GetConstructor(new Type[] { typeof(string) });
CustomAttributeBuilder taskNameAttributeBuilder =
new CustomAttributeBuilder(taskNameAttributeConstructor, new object[] { _taskName });
typeBuilder.SetCustomAttribute(taskNameAttributeBuilder);
// We're done. Create it.
Type taskType = typeBuilder.CreateType();
// Stash the XML in our static. We'll need it in DefinedTask later.
DefinedTaskDefinitions.Add(_taskName, XmlNode);
// Hook that up into NAnt.
TaskBuilder taskBuilder = new TaskBuilder(taskType.Assembly, taskType.FullName);
TypeFactory.TaskBuilders.Add(taskBuilder);
}
示例13: AssemblyAlgorithmIdAttributeTest
public AssemblyAlgorithmIdAttributeTest ()
{
//create a dynamic assembly with the required attribute
//and check for the validity
dynAsmName.Name = "TestAssembly";
dynAssembly = Thread.GetDomain ().DefineDynamicAssembly (
dynAsmName,AssemblyBuilderAccess.Run
);
// Set the required Attribute of the assembly.
Type attribute = typeof (AssemblyAlgorithmIdAttribute);
ConstructorInfo ctrInfo = attribute.GetConstructor (
new Type [] { typeof (AssemblyHashAlgorithm) }
);
CustomAttributeBuilder attrBuilder =
new CustomAttributeBuilder (
ctrInfo,
new object [1] { AssemblyHashAlgorithm.MD5 }
);
dynAssembly.SetCustomAttribute (attrBuilder);
object [] attributes = dynAssembly.GetCustomAttributes (true);
attr = attributes [0] as AssemblyAlgorithmIdAttribute;
}
示例14: Create
public static CodeCustomAttribute Create (Type attributeType, Type [] ctorArgTypes, CodeLiteral [] ctorArgs, MemberInfo [] members, CodeLiteral [] values)
{
ArrayList props = new ArrayList ();
ArrayList pvalues = new ArrayList ();
ArrayList fields = new ArrayList ();
ArrayList fvalues = new ArrayList ();
for (int i = 0; i < members.Length; i++) {
if (members [i] == null)
throw new ArgumentException (String.Format ("MemberInfo at {0} was null for type {1}.", i, attributeType));
if (members [i] is PropertyInfo) {
props.Add ((PropertyInfo) members [i]);
pvalues.Add (values [i].Value);
} else {
fields.Add ((FieldInfo) members [i]);
fvalues.Add (values [i].Value);
}
}
ConstructorInfo ci = attributeType.GetConstructor (ctorArgTypes);
CustomAttributeBuilder cab = new CustomAttributeBuilder (
ci, ctorArgs,
(PropertyInfo []) props.ToArray (typeof (PropertyInfo)), pvalues.ToArray (),
(FieldInfo []) fields.ToArray (typeof (FieldInfo)), fvalues.ToArray ());
CodeCustomAttribute attr = new CodeCustomAttribute (
cab, attributeType, ci, ctorArgs, members, values);
return attr;
}
示例15: ParseQueueTrigger
protected ParameterDescriptor ParseQueueTrigger(JObject trigger, Type triggerParameterType = null)
{
if (triggerParameterType == null)
{
triggerParameterType = typeof(string);
}
ConstructorInfo ctorInfo = typeof(QueueTriggerAttribute).GetConstructor(new Type[] { typeof(string) });
string queueName = (string)trigger["queueName"];
CustomAttributeBuilder attributeBuilder = new CustomAttributeBuilder(ctorInfo, new object[] { queueName });
string parameterName = (string)trigger["name"];
ParameterDescriptor triggerParameter = new ParameterDescriptor
{
Name = parameterName,
Type = triggerParameterType,
Attributes = ParameterAttributes.None,
CustomAttributes = new Collection<CustomAttributeBuilder>
{
attributeBuilder
}
};
return triggerParameter;
}