创建动态枚举并尝试引用它失败并显示 BindingFailure
我需要创建一个动态枚举,然后能够使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这实际上已记录在 MSDN 上:
然后文档继续说您可以订阅 AppDomain.AssemblyResolve 来解析动态程序集。
This is actually documented on MSDN now:
The docs then go on by saying that you can subscribe AppDomain.AssemblyResolve to resolve a dynamic assembly.
我不明白你想在那里做什么。有两个问题: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