當前位置: 首頁>>代碼示例>>C#>>正文


C# AppDomain.DefineDynamicAssembly方法代碼示例

本文整理匯總了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);
 }
開發者ID:jseijas,項目名稱:supido,代碼行數:15,代碼來源:TypeBuilderHelper.cs

示例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
				);
			}
		}
開發者ID:nick12344356,項目名稱:The-Basement,代碼行數:27,代碼來源:Emitter.cs

示例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");
        }
開發者ID:ZLLselfRedeem,項目名稱:zllinmitu,代碼行數:61,代碼來源:Program.cs

示例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;
 }
開發者ID:zeldafreak,項目名稱:Area51,代碼行數:8,代碼來源:MainWindow.xaml.cs

示例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);
		}
開發者ID:marchlud,項目名稱:nhibernate-core,代碼行數:9,代碼來源:DefaultProxyAssemblyBuilder.cs

示例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);
 }
開發者ID:master,項目名稱:plil,代碼行數:9,代碼來源:exe.cs

示例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>();
        }
開發者ID:destinyclown,項目名稱:VBF,代碼行數:11,代碼來源:EmitTranslator.cs

示例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);
        }
開發者ID:MilesBoulanger,項目名稱:game-creator,代碼行數:12,代碼來源:DotNetCompiler.cs

示例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");




        }
開發者ID:Geronimobile,項目名稱:DotNetExamIntro,代碼行數:49,代碼來源:Program.cs

示例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>();
        }
開發者ID:CurlyBrackets,項目名稱:DynamicLibrary,代碼行數:15,代碼來源:ClassDeserializer.cs

示例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);
 }
開發者ID:olympum,項目名稱:caffeine,代碼行數:15,代碼來源:PEemitter.cs

示例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" );
        }
開發者ID:ghostmonk,項目名稱:CSharpTutorials,代碼行數:39,代碼來源:AssemblyMaker.cs

示例13: DefineDynamicAssembly

		public AssemblyBuilder DefineDynamicAssembly(AppDomain appDomain, AssemblyName name)
		{
			return assembly ?? (assembly = appDomain.DefineDynamicAssembly(new AssemblyName(assemblyName), AssemblyBuilderAccess.RunAndSave));
		}
開發者ID:uQr,項目名稱:NHibernate.ProxyGenerators,代碼行數:4,代碼來源:SavingProxyAssemblyBuilder.cs

示例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 ();
	}
開發者ID:Profit0004,項目名稱:mono,代碼行數:14,代碼來源:AssemblyBuilderTest.cs

示例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);
            });
        }
開發者ID:dougrathbone,項目名稱:mbunit-v3,代碼行數:40,代碼來源:DynamicAssemblyBuilder.cs


注:本文中的System.AppDomain.DefineDynamicAssembly方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。