MSIL实用指南-生成接口


本篇讲解怎么样生成接口,即interface。

一、创建类型
创建一个接口类型依旧用ModuleBuilder的DefineType方法,但是它的第二个参数必须要有
TypeAttributes.Interface | TypeAttributes.Abstract。这样才能是一个接口类型。

实例代码:

TypeBuilder typeBuilder = moduleBuilder.DefineType(namespaceName + "." + typeName,
TypeAttributes.Public
| TypeAttributes.Interface
| TypeAttributes.Abstract
);

 

二、给接口添加继承父接口
按照C#语言规范,一个接口可以继承多个接口。在这里我们要用TypeBuilder的AddInterfaceImplementation
方法,参数是一个Type。

实例代码:

typeBuilder.AddInterfaceImplementation(typeof(System.IDisposable));
typeBuilder.AddInterfaceImplementation(typeof(System.IConvertible));

 

三、实现成员
C#的接口内的成员必须是public的,而且只能声明,不能实现。所以接口方法必须是
MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual ,
接口内属性的get和set方法必须也是如此。


完整的程序如下:

using System;
using System.Reflection;
using System.Reflection.Emit;

namespace LX1_ILDemo
{
    public class Demo22_InterfaceBuilder
    {
        public static void Generate()
        {
            /*
            public interface IMyDynamicInteface
            {
                int Number{get;set;}             
                int MyMethod(int multiplier);
            }
            */
            string binaryName = "Demo22_InterfaceBuilder.dll";
            string namespaceName = "LX1_ILDemo";
            string typeName = "DemoInterface";

            AssemblyName assemblyName = new AssemblyName(namespaceName);
            AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndSave);
            ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule(assemblyName.Name, binaryName);

            TypeBuilder typeBuilder = moduleBuilder.DefineType(namespaceName + "." + typeName,
                TypeAttributes.Public
                | TypeAttributes.Interface
                | TypeAttributes.Abstract
                );

            typeBuilder.AddInterfaceImplementation(typeof(System.IDisposable));
            typeBuilder.AddInterfaceImplementation(typeof(System.IConvertible));

            PropertyBuilder pbNumber = typeBuilder.DefineProperty(
                "Number",
                PropertyAttributes.HasDefault,
                typeof(int),
                null);


            MethodAttributes getSetAttr = MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual |
                MethodAttributes.SpecialName | MethodAttributes.HideBySig;

            MethodBuilder mbNumberGetAccessor = typeBuilder.DefineMethod(
                "get_Number",
                getSetAttr,
                typeof(int),
                Type.EmptyTypes);

            MethodBuilder mbNumberSetAccessor = typeBuilder.DefineMethod(
                "set_Number",
                getSetAttr,
                null,
                new Type[] { typeof(int) });

            pbNumber.SetGetMethod(mbNumberGetAccessor);
            pbNumber.SetSetMethod(mbNumberSetAccessor);

            MethodBuilder meth = typeBuilder.DefineMethod(
                "MyMethod",
                MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual,
                typeof(int),
                new Type[] { typeof(int) });

            Type t = typeBuilder.CreateType();

            assemblyBuilder.Save(binaryName);

            Console.WriteLine("创建接口IMyDynamicInteface完毕");
            Console.ReadKey();
        }
    }
}
View Code

 

posted @ 2018-04-06 15:06  Z语言  阅读(312)  评论(0编辑  收藏  举报