This site uses cookies to deliver our services. By using this site, you acknowledge that you have read and understand our Cookie and Privacy policy. Your use of Kontext website is subject to this policy. Allow Cookies and Dismiss


125 views 0 comments last modified about 11 years ago Raymond Tang

using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Threading;
//动态创建程序集 by 1987Raymond 2008.7.17


namespace Demo
    public interface ISayHello
        int SayHello();

    /// <summary>
    ///ReflectionOfDefineDynamicAssembly 的摘要说明
    /// </summary>
    public class ReflectionOfDefineDynamicAssembly
        private ISayHello sayHello = null;
        public ReflectionOfDefineDynamicAssembly()
            //TODO: 在此处添加构造函数逻辑

        private Assembly EmitAssembly(string sMsg)
            AssemblyName assemblyName = new AssemblyName("SayHelloAssembly");
            AssemblyBuilder newAssembly = Thread.GetDomain().DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
            ModuleBuilder newModule = newAssembly.DefineDynamicModule("SayHello");
            TypeBuilder myType = newModule.DefineType("MySayHello", TypeAttributes.Public | TypeAttributes.Class);
            Type[] paramTypes = new Type[0];//不传入参数
            Type returnType = typeof(int);
            MethodBuilder myMethod = myType.DefineMethod("SayHello", MethodAttributes.Public | MethodAttributes.Virtual, returnType, paramTypes);
            ILGenerator generator = myMethod.GetILGenerator();
            generator.EmitWriteLine("Hello " + sMsg);
            generator.Emit(OpCodes.Ldc_I4, 1);
            MethodInfo info = typeof(ISayHello).GetMethod("SayHello");
            myType.DefineMethodOverride(myMethod, info);
            return newAssembly;
        public void GenerateCode(string sMsg)
            Assembly theAssembly = EmitAssembly(sMsg);
            sayHello = (ISayHello)theAssembly.CreateInstance("MySayHello");

        //设置接口变量 如果不为空则
        public int DoSayHello(string sMsg)
            if (sayHello != null)
            return sayHello.SayHello();

    public class Test
        public static void Main()

        ReflectionOfDefineDynamicAssembly test = new ReflectionOfDefineDynamicAssembly();
        int i = test.DoSayHello("1987Raymond/果汁分你一半");


Related pages

Set AttachDbFilename as Relative Path in .NET Core

66 views   0 comments last modified about 2 months ago

.NET Framework, you can use |DataDirectory| to configure connection string when connecting to SQL Server database file via attach mode: AttachDbFilename=|DataDirectory|\dbname.mdf In .NET Core, you cannot directly set SQL Server Express connec...

View detail

Instantiate a Service in ConfigureServices Method in .NET Core

21 views   0 comments last modified about 2 months ago

.NET Core is built in with dependency injection. Usually method ConfigureServices in Startup class is used to register services in the container. The signature of the method looks like the following: public void ConfigureServices(IServiceC...

View detail

SQLite in .NET Core with Entity Framework Core

39 views   0 comments last modified about 2 months ago

SQLite is a self-contained and embedded SQL database engine. In .NET Core, Entity Framework Core provides APIs to work with SQLite. This page provides sample code to create a SQLite database using package Microsoft.EntityFrameworkCore.Sqlite . Create sample project ...

View detail

Graphics Programming and Image Processing in .NET Core 2.x

24 views   0 comments last modified about 2 months ago

In .NET Core 2.x, Windows Forms or WPF are not implemented since they are based on GDI+ and DirectX respectively in Windows. In .NET Core 3.0, there is plan to add Desktop Packs which includes UWP. WPF and Windows Forms. However, they will still be Windows-only. In .NET Core applications, you may...

View detail

Tuples in C# (4.x, 7.0, 7.1)

140 views   0 comments last modified about 6 months ago

What is a tuple? Tuple is an ordered sequence, fixed-size, immutable and of heterogeneous objects. Tuple has been existing in other languages such as F#, Python, Perl and etc. for a long time. It was first introduced into C# from C# 4.0 and has been evolving over time. Since C# 7.1, tuple...

View detail

Invoke Hadoop WebHDFS APIs in .NET Core

276 views   0 comments last modified about 6 months ago

Background Apache doesn't provide native official .NET APIs for Hadoop HDFS. The HTTP REST API supports the complete FileSystem / ...

View detail

Add comment

Please login first to add comments.  Log in New user?  Register

Comments (0)

No comments yet.