本文整理汇总了C#中System.AppDomain.DefineDynamicAssembly方法的典型用法代码示例。如果您正苦于以下问题:C# AppDomain.DefineDynamicAssembly方法的具体用法?C# AppDomain.DefineDynamicAssembly怎么用?C# AppDomain.DefineDynamicAssembly使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.AppDomain
的用法示例。
在下文中一共展示了AppDomain.DefineDynamicAssembly方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetClassTypeBuilder
/// <summary>
/// Creates a new TypeBuilder in the given domain
/// </summary>
/// <param name="domain">The domain.</param>
/// <param name="assemblyNameStr">The assembly name string.</param>
/// <param name="dynamicModuleName">Name of the dynamic module.</param>
/// <param name="typeName">Name of the type.</param>
/// <returns></returns>
public static TypeBuilder GetClassTypeBuilder(AppDomain domain, string assemblyNameStr, string dynamicModuleName, string typeName)
{
AssemblyName assemblyName = new AssemblyName(assemblyNameStr);
AssemblyBuilder assemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
ModuleBuilder module = assemblyBuilder.DefineDynamicModule(dynamicModuleName);
return module.DefineType(typeName, TypeAttributes.Public | TypeAttributes.Class);
}
示例2: AssemblyEmitter
public AssemblyEmitter( string assemblyName, bool canSave )
{
m_AssemblyName = assemblyName;
m_AppDomain = AppDomain.CurrentDomain;
m_AssemblyBuilder = m_AppDomain.DefineDynamicAssembly(
new AssemblyName( assemblyName ),
canSave ? AssemblyBuilderAccess.RunAndSave : AssemblyBuilderAccess.Run
);
if ( canSave )
{
m_ModuleBuilder = m_AssemblyBuilder.DefineDynamicModule(
assemblyName,
String.Format( "{0}.dll", assemblyName.ToLower() ),
false
);
}
else
{
m_ModuleBuilder = m_AssemblyBuilder.DefineDynamicModule(
assemblyName,
false
);
}
}
示例3: CreateMyAsm
// 调用者传入一个AppDomain类型
public static void CreateMyAsm(AppDomain curAppDomain)
{
// 建立通用的程序集特征
AssemblyName assemblyName = new AssemblyName();
assemblyName.Name = "MyAssembly";
assemblyName.Version = new Version("1.0.0.0");
// 在当前AppDomain(应用程序域)中创建一个新的程序集
AssemblyBuilder assembly =
curAppDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Save);
// 鉴于我们构造的是一个单文件程序集,模块的名字就是程序集的名字
ModuleBuilder module = assembly.DefineDynamicModule("MyAssembly", "MyAssembly.dll");
// 定义一个公共类“HelloWorld”
TypeBuilder helloWorldClass =
module.DefineType("MyAssembly.HelloWorld", TypeAttributes.Public);
// 定义一个私有字符串成员变量“theMessage”
FieldBuilder msgField =
helloWorldClass.DefineField("theMessage", Type.GetType("System.String"),
FieldAttributes.Private);
// 创建自定义的构造函数
Type[] constructorArgs = new Type[1];
constructorArgs[0] = typeof(string);
ConstructorBuilder constructor =
helloWorldClass.DefineConstructor(MethodAttributes.Public,
CallingConventions.Standard, constructorArgs);
ILGenerator constructorIL = constructor.GetILGenerator();
constructorIL.Emit(OpCodes.Ldarg_0);
Type objectClass = typeof(object);
ConstructorInfo superConstructor = objectClass.GetConstructor(new Type[0]);
constructorIL.Emit(OpCodes.Call, superConstructor);
constructorIL.Emit(OpCodes.Ldarg_0);
constructorIL.Emit(OpCodes.Ldarg_1);
constructorIL.Emit(OpCodes.Stfld, msgField);
constructorIL.Emit(OpCodes.Ret);
helloWorldClass.DefineDefaultConstructor(MethodAttributes.Public);
MethodBuilder getMsgMethod =
helloWorldClass.DefineMethod("GetMsg", MethodAttributes.Public, typeof(string), null);
ILGenerator methodIL = getMsgMethod.GetILGenerator();
methodIL.Emit(OpCodes.Ldarg_0);
methodIL.Emit(OpCodes.Ldfld, msgField);
methodIL.Emit(OpCodes.Ret);
MethodBuilder sayHiMethod = helloWorldClass.DefineMethod("SayHello", MethodAttributes.Public, null, null);
methodIL = sayHiMethod.GetILGenerator();
methodIL.EmitWriteLine("Hello form the HelloWorld class!");
methodIL.Emit(OpCodes.Ret);
helloWorldClass.CreateType();
assembly.Save("MyAssembly.dll");
}
示例4: MainWindow
public MainWindow()
{
Microsoft.CodeAnalysis.CSharp.Scrip
InitializeComponent();
otherDomain = AppDomain.CreateDomain("Other");
otherDomain.DefineDynamicAssembly(new AssemblyName("Fiddle.dll"), AssemblyBuilderAccess.RunAndCollect);
otherDomain.AssemblyLoad += Executor.CurrentDomain_AssemblyLoad;
}
示例5: DefineDynamicAssembly
public AssemblyBuilder DefineDynamicAssembly(AppDomain appDomain, AssemblyName name)
{
#if DEBUG
AssemblyBuilderAccess access = AssemblyBuilderAccess.RunAndSave;
#else
AssemblyBuilderAccess access = AssemblyBuilderAccess.Run;
#endif
return appDomain.DefineDynamicAssembly(name, access);
}
示例6: BeginModule
public void BeginModule(string ifile)
{
appdomain = System.Threading.Thread.GetDomain();
appname = getAssemblyName(filename);
appbuild = appdomain.DefineDynamicAssembly(appname, AssemblyBuilderAccess.Save, ".");
emodule = appbuild.DefineDynamicModule(filename + "_module", io.GetOutputFilename(), io.getGenDebug());
Guid g = System.Guid.Empty;
if (io.getGenDebug()) srcdoc = emodule.DefineDocument(ifile, g, g, g);
}
示例7: EmitTranslator
public EmitTranslator(AppDomain hostDomain, string name)
{
AssemblyName asmName = new AssemblyName(name);
m_assembly = hostDomain.DefineDynamicAssembly(asmName, AssemblyBuilderAccess.RunAndSave);
m_module = m_assembly.DefineDynamicModule(name + ".exe", true);
m_typeTable = new ExtensionTable<Type>();
m_methodTable = new ExtensionTable<MethodInfo>();
m_ctorTable = new ExtensionTable<ConstructorInfo>();
m_fieldTable = new ExtensionTable<FieldInfo>();
}
示例8: DotNetCompiler
public DotNetCompiler(LibraryContext context, string filename)
{
Context = context;
var fi = new FileInfo(filename);
Path = fi;
BuilderAppDomain = AppDomain.CurrentDomain;
AssemblyBuilder = BuilderAppDomain.DefineDynamicAssembly(new AssemblyName(fi.Name), AssemblyBuilderAccess.RunAndSave, fi.DirectoryName);
MainModule = AssemblyBuilder.DefineDynamicModule(fi.Name, fi.Name);
ScriptsType = MainModule.DefineType("Scripts", TypeAttributes.Class);
RoomsType = MainModule.DefineType("Rooms", TypeAttributes.Class);
}
示例9: CreateMyAsm
static void CreateMyAsm(AppDomain currAppDomain) {
AssemblyName assemblyName = new AssemblyName();
assemblyName.Name = "MyAssembly";
assemblyName.Version = new Version("1.0.0.0");
AssemblyBuilder assembly = currAppDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Save);
ModuleBuilder module = assembly.DefineDynamicModule("MyAssembly", "MyAssembly.dll");
TypeBuilder helloWorldClass = module.DefineType("MyAssembly.HelloWorld", TypeAttributes.Public);
FieldBuilder msgField = helloWorldClass.DefineField("theMessage", typeof(string), FieldAttributes.Private);
Type[] constructorArgs = new Type[1];
constructorArgs[0] = typeof(string);
ConstructorBuilder constructor = helloWorldClass.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, constructorArgs);
ILGenerator constructorIL = constructor.GetILGenerator();
constructorIL.Emit(OpCodes.Ldarg_0);
Type objectClass = typeof(object);
ConstructorInfo superConstructor = objectClass.GetConstructor(new Type[0]);
constructorIL.Emit(OpCodes.Call, superConstructor);
constructorIL.Emit(OpCodes.Ldarg_0);
constructorIL.Emit(OpCodes.Ldarg_1);
constructorIL.Emit(OpCodes.Stfld, msgField);
constructorIL.Emit(OpCodes.Ret);
helloWorldClass.DefineDefaultConstructor(MethodAttributes.Public);
MethodBuilder getMsgMethod = helloWorldClass.DefineMethod("GetMsg", MethodAttributes.Public, typeof(string), null);
ILGenerator methodIL = getMsgMethod.GetILGenerator();
methodIL.Emit(OpCodes.Ldarg_0);
methodIL.Emit(OpCodes.Ldfld, msgField);
methodIL.Emit(OpCodes.Ret);
MethodBuilder sayHiMethod = helloWorldClass.DefineMethod("SayHello", MethodAttributes.Public, null, null);
methodIL = sayHiMethod.GetILGenerator();
methodIL.EmitWriteLine("Hello from the HelloWorld class");
methodIL.Emit(OpCodes.Ret);
helloWorldClass.CreateType();
assembly.Save("MyAssembly.dll");
}
示例10: ClassDeserializer
public ClassDeserializer(ClassManager parent)
{
m_parent = parent;
var an = new AssemblyName()
{
Name = "HelloReflectionEmit"
};
m_ad = AppDomain.CurrentDomain;
m_ab = m_ad.DefineDynamicAssembly(an, System.Reflection.Emit.AssemblyBuilderAccess.RunAndSave);
m_mb = m_ab.DefineDynamicModule(an.Name, "Hello.exe");
m_stack = new Stack<ClassFrame>();
}
示例11: BeginModule
public void BeginModule(string fileName)
{
FileInfo f = new FileInfo(fileName);
appDomain = Thread.GetDomain();
asmName = new AssemblyName();
asmName.Name = f.Name;
asmBuilder = appDomain.DefineDynamicAssembly(
asmName,
AssemblyBuilderAccess.RunAndSave,
f.DirectoryName
);
modBuilder = asmBuilder.DefineDynamicModule(
asmName.Name,
asmName.Name);
}
示例12: Create
public static void Create( AppDomain domain, string asmName, string progName )
{
AssemblyName assemblyName = new AssemblyName();
assemblyName.Name = asmName;
assemblyName.Version = new Version("1.0.0.0");
AssemblyBuilder assembler = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Save);
ModuleBuilder moduleBuilder = assembler.DefineDynamicModule( asmName, asmName + ".dll" );
TypeBuilder helloWorldBuilder = moduleBuilder.DefineType(asmName + "." + progName, TypeAttributes.Public);
FieldBuilder fieldBuilder = helloWorldBuilder.DefineField("theMessage", Type.GetType("System.String"), FieldAttributes.Private);
Type[] constructorArgs = new Type[1];
constructorArgs[0] = typeof (string);
ConstructorBuilder constructor = helloWorldBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, constructorArgs);
ILGenerator constructorIl = constructor.GetILGenerator();
constructorIl.Emit(OpCodes.Ldarg_0);
Type objectClass = typeof(object);
ConstructorInfo superConstructor = objectClass.GetConstructor(new Type[0]);
constructorIl.Emit(OpCodes.Call, superConstructor);
constructorIl.Emit( OpCodes.Ldarg_0 );
constructorIl.Emit( OpCodes.Ldarg_1 );
constructorIl.Emit( OpCodes.Stfld, fieldBuilder );
constructorIl.Emit( OpCodes.Ret );
helloWorldBuilder.DefineDefaultConstructor(MethodAttributes.Public);
MethodBuilder methodBuilder = helloWorldBuilder.DefineMethod("GetMsg", MethodAttributes.Public, typeof (string), null);
ILGenerator methodIl = methodBuilder.GetILGenerator();
methodIl.Emit( OpCodes.Ldarg_0 );
methodIl.Emit( OpCodes.Ldfld, fieldBuilder );
methodIl.Emit( OpCodes.Ret );
MethodBuilder executor = helloWorldBuilder.DefineMethod("SayHello", MethodAttributes.Public, null, null);
methodIl = executor.GetILGenerator();
methodIl.EmitWriteLine("Hello from the HelloWorld class!");
methodIl.Emit(OpCodes.Ret);
helloWorldBuilder.CreateType();
assembler.Save( asmName + ".dll" );
}
示例13: DefineDynamicAssembly
public AssemblyBuilder DefineDynamicAssembly(AppDomain appDomain, AssemblyName name)
{
return assembly ?? (assembly = appDomain.DefineDynamicAssembly(new AssemblyName(assemblyName), AssemblyBuilderAccess.RunAndSave));
}
示例14: DefineDynamicAssembly
static void 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 ();
}
示例15: BuildAssembly
private void BuildAssembly(AppDomain appDomain, string dir, string filename)
{
// Prepare the assembly-level permissions.
PermissionSet requiredPermissions = new PermissionSet(null);
PermissionSet optionalPermissions = new PermissionSet(null);
PermissionSet refusedPermissions = new PermissionSet(null);
foreach (SecurityDeclaration securityDeclaration in assemblyDefinition.SecurityDeclarations)
{
switch (securityDeclaration.Action)
{
case Mono.Cecil.SecurityAction.RequestMinimum:
requiredPermissions = requiredPermissions.Union(securityDeclaration.PermissionSet);
break;
case Mono.Cecil.SecurityAction.RequestOptional:
optionalPermissions = optionalPermissions.Union(securityDeclaration.PermissionSet);
break;
case Mono.Cecil.SecurityAction.RequestRefuse:
refusedPermissions = refusedPermissions.Union(securityDeclaration.PermissionSet);
break;
}
}
// Build the dynamic assembly.
AssemblyBuilder assemblyBuilder = appDomain.DefineDynamicAssembly(
new AssemblyName(assemblyDefinition.Name.FullName),
AssemblyBuilderAccess.RunAndSave, dir,
requiredPermissions, optionalPermissions, refusedPermissions);
dynamicAssembly = new DynamicAssembly(assemblyBuilder, filename);
// TODO: Set entry point and assembly kind.
foreach (ModuleDefinition moduleDefinition in assemblyDefinition.Modules)
BuildModule(moduleDefinition);
metadataPass.Add(delegate
{
InitializeCustomAttributes(assemblyBuilder.SetCustomAttribute, assemblyDefinition.CustomAttributes);
});
}