本文整理汇总了C#中AppDomain.DefineDynamicAssembly方法的典型用法代码示例。如果您正苦于以下问题:C# AppDomain.DefineDynamicAssembly方法的具体用法?C# AppDomain.DefineDynamicAssembly怎么用?C# AppDomain.DefineDynamicAssembly使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类AppDomain
的用法示例。
在下文中一共展示了AppDomain.DefineDynamicAssembly方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DefineDynamicAssembly
static Assembly DefineDynamicAssembly (AppDomain domain)
{
AssemblyName assemblyName = new AssemblyName ();
assemblyName.Name = "MyDynamicAssembly";
AssemblyBuilder assemblyBuilder = domain.DefineDynamicAssembly (assemblyName, AssemblyBuilderAccess.Run);
ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule ("MyDynamicModule");
TypeBuilder typeBuilder = moduleBuilder.DefineType ("MyDynamicType", TypeAttributes.Public);
ConstructorBuilder constructorBuilder = typeBuilder.DefineConstructor (MethodAttributes.Public, CallingConventions.Standard, null);
ILGenerator ilGenerator = constructorBuilder.GetILGenerator ();
ilGenerator.EmitWriteLine ("MyDynamicType instantiated!");
ilGenerator.Emit (OpCodes.Ret);
typeBuilder.CreateType ();
return assemblyBuilder;
}
示例2: MyCreateCallee
private static Type MyCreateCallee(AppDomain domain)
{
AssemblyName myAssemblyName = new AssemblyName();
myAssemblyName.Name = "EmittedAssembly";
// Define a dynamic assembly in the current application domain.
AssemblyBuilder myAssembly =
domain.DefineDynamicAssembly(myAssemblyName,AssemblyBuilderAccess.Run);
// Define a dynamic module in this assembly.
ModuleBuilder myModuleBuilder = myAssembly.DefineDynamicModule("EmittedModule");
// Construct a 'TypeBuilder' given the name and attributes.
TypeBuilder myTypeBuilder = myModuleBuilder.DefineType("HelloWorld",
TypeAttributes.Public);
// Define a constructor of the dynamic class.
ConstructorBuilder myConstructor = myTypeBuilder.DefineConstructor(
MethodAttributes.Public, CallingConventions.Standard, new Type[]{typeof(String)});
ILGenerator myILGenerator = myConstructor.GetILGenerator();
myILGenerator.Emit(OpCodes.Ldstr, "Constructor is invoked");
myILGenerator.Emit(OpCodes.Ldarg_1);
MethodInfo myMethodInfo =
typeof(Console).GetMethod("WriteLine",new Type[]{typeof(string)});
myILGenerator.Emit(OpCodes.Call, myMethodInfo);
myILGenerator.Emit(OpCodes.Ret);
Type myType = typeof(MyAttribute);
ConstructorInfo myConstructorInfo = myType.GetConstructor(new Type[]{typeof(object)});
try
{
CustomAttributeBuilder methodCABuilder = new CustomAttributeBuilder (myConstructorInfo, new object [] { TypeCode.Double } );
myConstructor.SetCustomAttribute(methodCABuilder);
}
catch(ArgumentNullException ex)
{
Console.WriteLine("The following exception has occured : "+ex.Message);
}
catch(Exception ex)
{
Console.WriteLine("The following exception has occured : "+ex.Message);
}
return myTypeBuilder.CreateType();
}
示例3: CreateCaller
} //CreateCallee()
// Create the caller transient dynamic assembly.
private static Type CreateCaller(AppDomain appDomain,
AssemblyBuilderAccess access,
Type helloWorldClass)
{
// Create a simple name for the caller assembly.
AssemblyName assemblyName = new AssemblyName();
assemblyName.Name = CALLINGASSEMBLYNAME;
// Create the caller dynamic assembly.
AssemblyBuilder assembly = appDomain.DefineDynamicAssembly(assemblyName, access);
// Create a dynamic module named "CallerModule" in the caller assembly.
ModuleBuilder module;
if (access == AssemblyBuilderAccess.Run)
{
module = assembly.DefineDynamicModule("EmittedCallerModule");
}
else
{
module = assembly.DefineDynamicModule("EmittedCallerModule", "EmittedCallerModule.exe");
}
// Define a public class named MainClass.
TypeBuilder mainClass = module.DefineType("MainClass", TypeAttributes.Public);
// Create the method with name "main".
MethodAttributes methodAttributes = (MethodAttributes.Static | MethodAttributes.Public);
MethodBuilder mainMethod = mainClass.DefineMethod("Main", methodAttributes, null, null);
// Generate IL for the method.
ILGenerator mainIL = mainMethod.GetILGenerator();
// Define the greeting string constant and emit it. This gets consumed by the constructor.
mainIL.Emit(OpCodes.Ldstr, "HelloWorld from dynamically created caller");
// Use the provided "HelloWorld" class
// Find the constructor for the "HelloWorld" class.
Type[] constructorArgs = { typeof(string) };
ConstructorInfo constructor = helloWorldClass.GetConstructor(constructorArgs);
// Instantiate the "HelloWorld" class.
mainIL.Emit(OpCodes.Newobj, constructor);
// Find the "GetGreeting" method of the "HelloWorld" class.
MethodInfo getGreetingMethod = helloWorldClass.GetMethod("GetGreeting");
// Call the "GetGreeting" method to obtain the greeting.
mainIL.Emit(OpCodes.Call, getGreetingMethod);
// Write the greeting to the console.
MethodInfo writeLineMethod = typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) });
mainIL.Emit(OpCodes.Call, writeLineMethod);
//mainIL.EmitWriteLine(
mainIL.Emit(OpCodes.Ret);
// Bake the class. You can now create instances of this class.
return(mainClass.CreateType());
} //CreateCaller()
示例4: CreateCallee
} //Main()
// Create the callee transient dynamic assembly.
private static Type CreateCallee(AppDomain appDomain, AssemblyBuilderAccess access)
{
// Create a simple name for the callee assembly.
AssemblyName assemblyName = new AssemblyName();
assemblyName.Name = EMITTEDASSEMBLYNAME;
// Create the callee dynamic assembly.
AssemblyBuilder assembly = appDomain.DefineDynamicAssembly(assemblyName, access);
// Create a dynamic module named "CalleeModule" in the callee assembly.
ModuleBuilder module;
if (access == AssemblyBuilderAccess.Run)
{
module = assembly.DefineDynamicModule("EmittedModule");
}
else
{
module = assembly.DefineDynamicModule("EmittedModule", "EmittedModule.mod");
}
// Define a public class named "HelloWorld" in the assembly.
TypeBuilder helloWorldClass = module.DefineType("HelloWorld", TypeAttributes.Public);
// Define a private string field named "Greeting" in the type.
FieldBuilder greetingField = helloWorldClass.DefineField("Greeting",
typeof(string),
FieldAttributes.Private);
// Create the constructor. It requires a string which it stores in greetingField.
Type[] constructorArgs = { typeof(string) };
ConstructorBuilder constructor = helloWorldClass.DefineConstructor(
MethodAttributes.Public, CallingConventions.Standard, constructorArgs);
// Generate IL for the method. The constructor calls its superclass
// constructor. The constructor stores its argument in the private field.
ILGenerator constructorIL = constructor.GetILGenerator();
constructorIL.Emit(OpCodes.Ldarg_0);
ConstructorInfo superConstructor = typeof(Object).GetConstructor(new Type[0]);
constructorIL.Emit(OpCodes.Call, superConstructor);
constructorIL.Emit(OpCodes.Ldarg_0);
constructorIL.Emit(OpCodes.Ldarg_1);
// Store string argument in field.
constructorIL.Emit(OpCodes.Stfld, greetingField);
constructorIL.Emit(OpCodes.Ret);
// Create the GetGreeting method.
MethodBuilder getGreetingMethod = helloWorldClass.DefineMethod("GetGreeting",
MethodAttributes.Public,
typeof(string),
null);
// Generate IL for GetGreeting.
ILGenerator methodIL = getGreetingMethod.GetILGenerator();
methodIL.Emit(OpCodes.Ldarg_0);
methodIL.Emit(OpCodes.Ldfld, greetingField);
methodIL.Emit(OpCodes.Ret);
// Bake the class HelloWorld.
return(helloWorldClass.CreateType());
} //CreateCallee()
示例5: PrepareAssembly
//==== Helpers ===================================================
// Prepare an assembly
private void PrepareAssembly( string typeName, bool bSave )
{
AssemblyBuilderAccess acc = bSave ? AssemblyBuilderAccess.Save:AssemblyBuilderAccess.Run;
AssemblyName aName = new AssemblyName();
aName.Name = typeName;
appdomain = AppDomain.CurrentDomain;
assemblybuilder = appdomain.DefineDynamicAssembly( aName, acc );
modulebuilder = assemblybuilder.DefineDynamicModule( typeName+".dll" );
typebuilder = modulebuilder.DefineType( typeName, TypeAttributes.Public );
}