创建动态枚举并尝试引用它失败并显示 BindingFailure

发布于 2024-08-07 01:35:34 字数 1259 浏览 4 评论 0原文

我需要创建一个动态枚举,然后能够使用 Type.GetType() 获取类型。这可能吗?

下面的代码将创建一个动态枚举,并尝试使用它的限定名称。如果我首先存储程序集(使用 AssemblyBuilderAccess.RunAndSave),这很好。但是,如果我单独使用 AssemblyBuilderAccess.Run,​​这是不可能的;发生 BindingFailure 错误;无法找到程序集。我的印象是,“运行”选项将允许创建和使用,而无需实际存储程序集(或访问不同的构建器)。

(注意:下面的 Type.GetType() 代码用法不是我的。我无法更改该代码。)

如何在不存储程序集的情况下创建动态枚举并引用它?

        private Type CreateType()
        {          
        // Define the assembly.
        System.Reflection.Emit.AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(new System.Reflection.AssemblyName("temporaryAssembly"), AssemblyBuilderAccess.Run);

        // Actually create it.
        System.Reflection.Emit.ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("temporaryAssembly");

        // Create the enum.
        System.Reflection.Emit.EnumBuilder enumBuilder = moduleBuilder.DefineEnum("Temp", System.Reflection.TypeAttributes.Public, typeof(int));

        /* Populate the enum. */

                    return enumBuilder.CreateType();
        }

        private void DoStuff()
        {
                    Type t = CreateType();
                    Type createAnotherOfSameType = Type.GetType(t.AssemblyQualifiedName);
        }/

I need to create a dynamic enum and then be able to get the type using Type.GetType(). Is this possible?

The below code will create a dynamic enum, and attempt to use it's qualifying name. This is fine if I first store the assembly (using AssemblyBuilderAccess.RunAndSave). However, this is not possible if I'm solely using AssemblyBuilderAccess.Run; a BindingFailure error occur; unable to find the assembly. My impression was that the Run option would allow creation and usage without having to actually store the assembly (or having access to the different Builders).

(Note: The below code usage of Type.GetType() is not mine. I cannot change that code.)

How can I, without storing the assembly, create a dynamic enum and reference it?

        private Type CreateType()
        {          
        // Define the assembly.
        System.Reflection.Emit.AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(new System.Reflection.AssemblyName("temporaryAssembly"), AssemblyBuilderAccess.Run);

        // Actually create it.
        System.Reflection.Emit.ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("temporaryAssembly");

        // Create the enum.
        System.Reflection.Emit.EnumBuilder enumBuilder = moduleBuilder.DefineEnum("Temp", System.Reflection.TypeAttributes.Public, typeof(int));

        /* Populate the enum. */

                    return enumBuilder.CreateType();
        }

        private void DoStuff()
        {
                    Type t = CreateType();
                    Type createAnotherOfSameType = Type.GetType(t.AssemblyQualifiedName);
        }/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感受沵的脚步 2024-08-14 01:35:34

这实际上已记录在 MSDN 上:

GetType 仅适用于从磁盘加载的程序集。如果你调用 GetType
查找使用以下定义的动态程序集中定义的类型
System.Reflection.Emit 服务,您可能会得到不一致的行为。
该行为取决于动态程序集是否持久,
也就是说,使用 RunAndSave 或 Save 访问模式创建
System.Reflection.Emit.AssemblyBuilderAccess 枚举。 [...] 如果
调用 GetType 时程序集尚未保存到磁盘,该方法
返回空值。 GetType 不理解瞬态动态
组件;因此,调用 GetType 来检索 a 中的类型
瞬态动态装配返回 null。

然后文档继续说您可以订阅 AppDomain.AssemblyResolve 来解析动态程序集。

This is actually documented on MSDN now:

GetType only works on assemblies loaded from disk. If you call GetType
to look up a type defined in a dynamic assembly defined using the
System.Reflection.Emit services, you might get inconsistent behavior.
The behavior depends on whether the dynamic assembly is persistent,
that is, created using the RunAndSave or Save access modes of the
System.Reflection.Emit.AssemblyBuilderAccess enumeration. [...] If the
assembly has not been saved to disk when GetType is called, the method
returns null. GetType does not understand transient dynamic
assemblies; therefore, calling GetType to retrieve a type in a
transient dynamic assembly returns null.

The docs then go on by saying that you can subscribe AppDomain.AssemblyResolve to resolve a dynamic assembly.

守不住的情 2024-08-14 01:35:34

我不明白你想在那里做什么。有两个问题:1)第二行只是尝试再次获取对 Type 的引用,该引用将与第一行相同;这不是你得到的一个实例。 2) IIRC,对于内存中类型/程序集,AssemblyQualifiedName 为 null。

-奥辛

I don't understand what you're trying to do there. Two problems: 1) the second line is just trying to get a reference to the Type again, which will be the same reference as the first line; it's not an instance you're getting. 2) IIRC, AssemblyQualifiedName is null for an in-memory type/assembly.

-Oisin

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文