本文整理汇总了C#中System.Reflection.Emit.AssemblyBuilder.SetCustomAttribute方法的典型用法代码示例。如果您正苦于以下问题:C# AssemblyBuilder.SetCustomAttribute方法的具体用法?C# AssemblyBuilder.SetCustomAttribute怎么用?C# AssemblyBuilder.SetCustomAttribute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Reflection.Emit.AssemblyBuilder
的用法示例。
在下文中一共展示了AssemblyBuilder.SetCustomAttribute方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: EmitInterfaceImplementorBase
static EmitInterfaceImplementorBase()
{
if (IsDirty && SaveCache)
{
AppDomain.CurrentDomain.ProcessExit += (sender, args) => AssemblyBuilder.Save(AsmFileName);
}
ResolveMethodInfo = null;
foreach (var methodInfo in typeof(AppScope).GetMethods())
{
if (methodInfo.Name == "Resolve" && methodInfo.IsGenericMethod)
{
ResolveMethodInfo = methodInfo;
break;
}
}
var idx = 0;
foreach (var asmFileName in Directory.EnumerateFiles(".", AsmFileName + "*.dll"))
{
idx++;
try
{
var asm1 = Assembly.LoadFrom(asmFileName);
foreach (var type in asm1.GetTypes())
{
var iface = type.GetInterfaces().FirstOrDefault();
if (iface != null) Types[iface] = type;
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
AsmFileName = string.Format("{0}.{1}.dll", AsmFileName, idx);
AssemblyBuilder =
AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName(AsmFileName)
{
Version = new Version(1, 0, 0, 0),
VersionCompatibility = AssemblyVersionCompatibility.SameMachine,
}, AssemblyBuilderAccess.RunAndSave);
//AssemblyBuilder.
//AssemblyBuilder.DefineVersionInfoResource(AsmFileName, "1.0.0.0", "Zen", "(c) 2013 by FruT", "ZenCore");
var verAttrType = typeof(AssemblyVersionAttribute); //("1.0.0.0")
var verAttrCtor = verAttrType.GetConstructor(new[] { typeof(string) });
var attrBuilder = new CustomAttributeBuilder(verAttrCtor, new object[] { "1.0.0.0" });
AssemblyBuilder.SetCustomAttribute(attrBuilder);
var fverAttrType = typeof(AssemblyVersionAttribute); //("1.0.0.0")
var fverAttrCtor = fverAttrType.GetConstructor(new[] { typeof(string) });
var fattrBuilder = new CustomAttributeBuilder(fverAttrCtor, new object[] { "1.0.0.0" });
AssemblyBuilder.SetCustomAttribute(fattrBuilder);
ModuleBuilder = AssemblyBuilder.DefineDynamicModule("EmitedImplementors", AsmFileName);
}
示例2: 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;
}
示例3: 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);
}
}
示例4: Emitter
public Emitter(EmitterOptions options)
{
_options = options;
_assemblyName = new AssemblyName(_options.AssemblyName);
_assembly = AppDomain.CurrentDomain.DefineDynamicAssembly(_assemblyName, AssemblyBuilderAccess.RunAndSave);// TODO: temp for debugging .RunAndCollect);
if (_options.DebugOn)
_assembly.SetCustomAttribute
(
new CustomAttributeBuilder
(
typeof(DebuggableAttribute).GetConstructor
(
new System.Type[] { typeof(DebuggableAttribute.DebuggingModes) }
),
new object[]
{
DebuggableAttribute.DebuggingModes.DisableOptimizations |
DebuggableAttribute.DebuggingModes.Default
}
)
);
_module = _assembly.DefineDynamicModule(_assemblyName.Name, _assemblyName.Name + ".dll", _options.DebugOn);
if (_options.DebugOn)
_symbolWriter = _module.DefineDocument(_options.SourceFileName, Guid.Empty, Guid.Empty, Guid.Empty);
_tupleToNative = new Dictionary<Type.TupleType, System.Type>();
}
示例5: AddDebuggingAttribute
private static void AddDebuggingAttribute(AssemblyBuilder assembly)
{
var debugAttribute = typeof(DebuggableAttribute);
var debugConstructor = debugAttribute.GetConstructor(
new Type[] { typeof(DebuggableAttribute.DebuggingModes) });
var debugBuilder = new CustomAttributeBuilder(
debugConstructor, new object[] {
DebuggableAttribute.DebuggingModes.DisableOptimizations |
DebuggableAttribute.DebuggingModes.Default });
assembly.SetCustomAttribute(debugBuilder);
}
示例6: ApplyTo
public static void ApplyTo(AssemblyBuilder assemblyBuilder, Dictionary<CacheKey, string> mappings)
{
using (var stream = new MemoryStream())
{
var formatter = new BinaryFormatter();
formatter.Serialize(stream, mappings);
var bytes = stream.ToArray();
var attributeBuilder = new CustomAttributeBuilder(constructor, new object[] { bytes });
assemblyBuilder.SetCustomAttribute(attributeBuilder);
}
}
示例7: EmitAttribute
private void EmitAttribute(AssemblyBuilder parent, CodeAttributeDeclaration attribute)
{
var type = (Type) attribute.AttributeType.UserData[Parser.RawData];
var vals = new object[attribute.Arguments.Count];
var args = new Type[vals.Length];
for (int i = 0; i < vals.Length; i++)
{
vals[i] = ((CodePrimitiveExpression) attribute.Arguments[i].Value).Value;
args[i] = vals[i].GetType();
}
var ctor = type.GetConstructor(args);
var builder = new CustomAttributeBuilder(ctor, vals);
parent.SetCustomAttribute(builder);
}
示例8: Emitter
public Emitter(EmitterOptions options)
{
_options = options;
//// TODO: setup separate app domain with appropriate cache path, shadow copying etc.
//var domainName = "plan" + DateTime.Now.Ticks.ToString();
//var domain = AppDomain.CreateDomain(domainName);
_assemblyName = new AssemblyName(_options.AssemblyName);
_assembly =
AppDomain.CurrentDomain.DefineDynamicAssembly
(
_assemblyName,
_options.DebugOn ? AssemblyBuilderAccess.RunAndSave : AssemblyBuilderAccess.RunAndCollect
);
if (_options.DebugOn)
_assembly.SetCustomAttribute
(
new CustomAttributeBuilder
(
typeof(DebuggableAttribute).GetConstructor
(
new System.Type[] { typeof(DebuggableAttribute.DebuggingModes) }
),
new object[]
{
DebuggableAttribute.DebuggingModes.DisableOptimizations |
DebuggableAttribute.DebuggingModes.Default
}
)
);
_module = _assembly.DefineDynamicModule(_assemblyName.Name, _assemblyName.Name + ".dll", _options.DebugOn);
if (_options.DebugOn)
_symbolWriter = _module.DefineDocument(_options.SourceFileName, Guid.Empty, Guid.Empty, Guid.Empty);
_tupleToNative = new Dictionary<TupleType, System.Type>();
}
示例9: SetPIAAttributeOnAssembly
[System.Security.SecurityCritical] // auto-generated
private static void SetPIAAttributeOnAssembly(AssemblyBuilder asmBldr, Object typeLib)
{
IntPtr pAttr = IntPtr.Zero;
_TYPELIBATTR Attr;
ITypeLib pTLB = (ITypeLib)typeLib;
int Major = 0;
int Minor = 0;
// Retrieve the PrimaryInteropAssemblyAttribute constructor.
Type []aConsParams = new Type[2] {typeof(int), typeof(int)};
ConstructorInfo PIAAttrCons = typeof(PrimaryInteropAssemblyAttribute).GetConstructor(aConsParams);
// Retrieve the major and minor version from the typelib.
try
{
pTLB.GetLibAttr(out pAttr);
Attr = (_TYPELIBATTR)Marshal.PtrToStructure(pAttr, typeof(_TYPELIBATTR));
Major = Attr.wMajorVerNum;
Minor = Attr.wMinorVerNum;
}
finally
{
// Release the typelib attributes.
if (pAttr != IntPtr.Zero)
pTLB.ReleaseTLibAttr(pAttr);
}
// Create an instance of the custom attribute builder.
Object[] aArgs = new Object[2] {Major, Minor};
CustomAttributeBuilder PIACABuilder = new CustomAttributeBuilder(PIAAttrCons, aArgs);
// Set the PrimaryInteropAssemblyAttribute on the assembly builder.
asmBldr.SetCustomAttribute(PIACABuilder);
}
示例10: SetTypeLibVersionAttribute
[System.Security.SecurityCritical] // auto-generated
private static void SetTypeLibVersionAttribute(AssemblyBuilder asmBldr, Object typeLib)
{
Type []aConsParams = new Type[2] {typeof(int), typeof(int)};
ConstructorInfo TypeLibVerCons = typeof(TypeLibVersionAttribute).GetConstructor(aConsParams);
// Get the typelib version
int major;
int minor;
Marshal.GetTypeLibVersion((ITypeLib)typeLib, out major, out minor);
// Create an instance of the custom attribute builder.
Object[] aArgs = new Object[2] {major, minor};
CustomAttributeBuilder TypeLibVerBuilder = new CustomAttributeBuilder(TypeLibVerCons, aArgs);
// Set the attribute on the assembly builder.
asmBldr.SetCustomAttribute(TypeLibVerBuilder);
}
示例11: SetImportedFromTypeLibAttrOnAssembly
[System.Security.SecurityCritical] // auto-generated
private static void SetImportedFromTypeLibAttrOnAssembly(AssemblyBuilder asmBldr, Object typeLib)
{
// Retrieve the ImportedFromTypeLibAttribute constructor.
Type []aConsParams = new Type[1] {typeof(String)};
ConstructorInfo ImpFromComAttrCons = typeof(ImportedFromTypeLibAttribute).GetConstructor(aConsParams);
// Retrieve the name of the typelib.
String strTypeLibName = Marshal.GetTypeLibName((ITypeLib)typeLib);
// Create an instance of the custom attribute builder.
Object[] aArgs = new Object[1] {strTypeLibName};
CustomAttributeBuilder ImpFromComCABuilder = new CustomAttributeBuilder(ImpFromComAttrCons, aArgs);
// Set the ImportedFromTypeLibAttribute on the assembly builder.
asmBldr.SetCustomAttribute(ImpFromComCABuilder);
}
示例12: SetGuidAttributeOnAssembly
[System.Security.SecurityCritical] // auto-generated
private static void SetGuidAttributeOnAssembly(AssemblyBuilder asmBldr, Object typeLib)
{
// Retrieve the GuidAttribute constructor.
Type []aConsParams = new Type[1] {typeof(String)};
ConstructorInfo GuidAttrCons = typeof(GuidAttribute).GetConstructor(aConsParams);
// Create an instance of the custom attribute builder.
Object[] aArgs = new Object[1] {Marshal.GetTypeLibGuid((ITypeLib)typeLib).ToString()};
CustomAttributeBuilder GuidCABuilder = new CustomAttributeBuilder(GuidAttrCons, aArgs);
// Set the GuidAttribute on the assembly builder.
asmBldr.SetCustomAttribute(GuidCABuilder);
}
示例13: CodegenUnit
internal CodegenUnit(AssemblyName asmName)
{
var fileName = asmName.Name + ".dll";
var pdbName = asmName + ".pdb";
// so that we can run multiple tests at once and not lose the info
if (UnitTest.CurrentTest != null)
{
fileName = asmName + ", " + UnitTest.PersistentId + ".dll";
pdbName = asmName + ".pdb";
}
#if TRACE
try
{
_asm = AppDomain.CurrentDomain.DefineDynamicAssembly(asmName, AssemblyBuilderAccess.RunAndSave);
_mod = _asm.DefineDynamicModule(fileName, true);
// Mark generated code as debuggable.
// See http://blogs.msdn.com/jmstall/archive/2005/02/03/366429.aspx for explanation.
var daCtor = typeof(DebuggableAttribute).GetConstructor(new []{typeof(DebuggableAttribute.DebuggingModes)});
var daBuilder = new CustomAttributeBuilder(daCtor, new object[] {
DebuggableAttribute.DebuggingModes.DisableOptimizations |
DebuggableAttribute.DebuggingModes.Default });
_asm.SetCustomAttribute(daBuilder);
// Mark generated code as non-user code.
// See http://stackoverflow.com/questions/1423733/how-to-tell-if-a-net-assembly-is-dynamic for explanation.
var cgCtor = typeof(CompilerGeneratedAttribute).GetConstructor(Type.EmptyTypes);
var cgBuilder = new CustomAttributeBuilder(cgCtor, new object []{});
_asm.SetCustomAttribute(cgBuilder);
var hasAlreadyBeenDumped = false;
Action dumpAssembly = () =>
{
if (!hasAlreadyBeenDumped && _asm != null)
{
try
{
// todo. before dumping make sure that all types are completed or else the dump will simply crash
// this is a complex task, but it needs to be resolved for generic case
_asm.Save(fileName);
}
catch (Exception ex)
{
var trace = String.Format("Codegen unit '{0}' has failed to dump the asm:{1}{2}",
asmName.FullName, Environment.NewLine, ex);
Log.WriteLine(trace);
SafetyTools.SafeDo(() =>
{
File.WriteAllText(fileName, trace);
File.Delete(pdbName);
});
}
finally
{
hasAlreadyBeenDumped = true;
}
}
};
_dumpAssembly = dumpAssembly;
// do not use DomainUnload here because it never gets fired for default domain
// however, we need not to neglect because R#'s unit-test runner never exits process
AppDomain.CurrentDomain.DomainUnload += (o, e) => dumpAssembly();
AppDomain.CurrentDomain.ProcessExit += (o, e) => dumpAssembly();
AppDomain.CurrentDomain.UnhandledException += (o, e) => dumpAssembly();
}
catch (Exception ex)
{
var trace = String.Format("Codegen unit '{0}' has failed to initialize:{1}{2}",
asmName.FullName, Environment.NewLine, ex);
Log.WriteLine(trace);
SafetyTools.SafeDo(() =>
{
File.WriteAllText(fileName, trace);
File.Delete(pdbName);
});
throw;
}
#else
_asm = AppDomain.CurrentDomain.DefineDynamicAssembly(asmName, AssemblyBuilderAccess.Run);
_asm.SetCustomAttribute(new CustomAttributeBuilder(typeof(CompilerGeneratedAttribute).GetConstructor(Type.EmptyTypes), new object[] { }));
_mod = _asm.DefineDynamicModule(fileName, false);
#endif
}
示例14: ApplyAssemblyAttributes
/// <summary>
/// Applies custom attributes to generated assembly.
/// </summary>
/// <param name="assembly">Dynamic assembly to apply attributes to.</param>
private void ApplyAssemblyAttributes(AssemblyBuilder assembly)
{
assembly.SetCustomAttribute(ReflectionUtils.CreateCustomAttribute(typeof(ApplicationNameAttribute), applicationName));
assembly.SetCustomAttribute(ReflectionUtils.CreateCustomAttribute(typeof(ApplicationActivationAttribute), activationMode));
if (applicationId != null)
{
assembly.SetCustomAttribute(ReflectionUtils.CreateCustomAttribute(typeof(ApplicationIDAttribute), applicationId));
}
if (description != null)
{
assembly.SetCustomAttribute(ReflectionUtils.CreateCustomAttribute(typeof(DescriptionAttribute), description));
assembly.SetCustomAttribute(ReflectionUtils.CreateCustomAttribute(typeof(AssemblyDescriptionAttribute), description));
}
if (accessControl != null)
{
assembly.SetCustomAttribute(ReflectionUtils.CreateCustomAttribute(accessControl));
}
if (applicationQueuing != null)
{
assembly.SetCustomAttribute(ReflectionUtils.CreateCustomAttribute(applicationQueuing));
}
if (roles != null)
{
foreach (SecurityRoleAttribute role in roles)
{
assembly.SetCustomAttribute(ReflectionUtils.CreateCustomAttribute(typeof(SecurityRoleAttribute),
new object[] { role.Role }, role));
}
}
}
示例15: RegexTypeCompiler
internal RegexTypeCompiler(AssemblyName an, CustomAttributeBuilder[] attribs, String resourceFile, Evidence evidence) {
new ReflectionPermission(PermissionState.Unrestricted).Assert();
try {
Debug.Assert(an != null, "AssemblyName should not be null");
_assembly = AppDomain.CurrentDomain.DefineDynamicAssembly(an, AssemblyBuilderAccess.RunAndSave, evidence);
_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();
}
}