从 Type 转换为 TypeBuilder [英] Convert from Type to TypeBuilder

查看:26
本文介绍了从 Type 转换为 TypeBuilder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行时创建一个类,其中一些类型已经在 ModuleBuilder 中创建,我想重用它们,但我只有 Type 而没有 TypeBuilder(这是我需要的才能更改它)

I'm creating a class on runtime and some of the types are already created inside the ModuleBuilder and I would like to reused them, but I only have the Type and not the TypeBuilder (Which is what I need in order to change it)

有没有办法从 Type 转换为 TypeBuilder?

Is there a way to convert from Type to TypeBuilder?

Type moduleType = ModuleBuilder.GetType(inXmlTemplateProperty.PropertyName);
        if (moduleType == null)
        {
            TypeBuilder newClass = ModuleBuilder.DefineType(inXmlTemplateProperty.PropertyName,
                                                            TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.Serializable);

            newClass.SetClassAttributes(inXmlTemplateProperty);

            return newClass;
        }

        return (TypeBuilder)moduleType;

欢迎提出任何建议,提前致谢.

Any suggestion is welcome, thanks in advance.

推荐答案

TypeBuilder 是 Type 的派生类型(通过中间类 TypeInfo).

TypeBuilder is a derived type of Type (via intermediate class TypeInfo).

因此您可以将 TypeBuilder 转换为 Type.但反过来不行.

So you can cast a TypeBuilder into a Type. But not the other way around.

您需要使用最高通用级别的抽象(类型).

You need to use the highest common level of abstraction (Type).

不要将 moduleType 转换为 TypeBuilder.稍后,您将能够使用如下代码检查该类型是否已创建:

Do not cast moduleType into a TypeBuilder. Later on, you will be able to check if the type has been already created or not, using code like that :

class Program
{
    private static Type GetSomeTypePending(string typeName)
    {
        AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName("toto.dll"), AssemblyBuilderAccess.RunAndSave);
        ModuleBuilder mb = ab.DefineDynamicModule("toto.dll");
        TypeBuilder tb = mb.DefineType(typeName);
        return tb;
    }

    private static Type GetSomeTypeCreated(string typeName)
    {
        AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName("toto.dll"), AssemblyBuilderAccess.RunAndSave);
        ModuleBuilder mb = ab.DefineDynamicModule("toto.dll");
        TypeBuilder tb = mb.DefineType(typeName);
        tb.CreateType(); // do not return the created type, 
        return tb;       // return the typebuilder
    }

    static void Main(string[] args)
    {
        Type foo = GetSomeTypePending("tutu");
        Type foo2 = GetSomeTypeCreated("tutu");
        if (IsPendingTypeBuilder(foo))
        {
            Console.WriteLine("foo not yet created");
        }
        else
        {
            Console.WriteLine("foo created");
        }
        if (IsPendingTypeBuilder(foo2))
        {
            Console.WriteLine("foo2 not yet created");
        }
        else
        {
            Console.WriteLine("foo2 created");
        }
    }

    private static bool IsPendingTypeBuilder(Type foo)
    {
        return (foo is TypeBuilder) && !((foo as TypeBuilder).IsCreated());
    }
}

这篇关于从 Type 转换为 TypeBuilder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆