我正在使用TypeBuilder
创建类型。然后将这些类型用作泛型类中的参数。我想要做的是从名称中创建Type
。泛型参数是动态的这一事实似乎是一个障碍。
static public class DynamicTypeTest
{
public class Generic<T> { }
static public void Test()
{
Type dynamicType = createDynamicType();
Type genericType = typeof(Generic<>);
// Generic<DynamicType>
Type genericDynamicType = genericType.MakeGenericType(new Type[]{dynamicType});
Debug.Assert(TypeFromName(genericDynamicType.FullName) == genericDynamicType); // fail
Debug.Assert(TypeFromName(genericDynamicType.AssemblyQualifiedName) == genericDynamicType); // fail
Debug.Assert(TypeFromName(genericDynamicType.Name) == genericDynamicType); // fail
// Generic<int>
Type genericIntType = genericType.MakeGenericType(new Type[] { typeof(int) });
Debug.Assert(TypeFromName(genericIntType.FullName) == genericIntType); // This succeeds. Replacing 'int' with a Type defined in another project also works
}
// This is the essence of what I want to do
static private Type TypeFromName(string name)
{
return Assembly.GetExecutingAssembly().GetType(name);
}
static private Type createDynamicType()
{
AssemblyName assemblyName = new AssemblyName("DynamicAssembly");
AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("DynamicModule");
TypeAttributes typeAttributes = TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.AutoClass | TypeAttributes.AnsiClass;
TypeBuilder typeBuilder = moduleBuilder.DefineType("DynamicType", typeAttributes, typeof(System.Object));
return typeBuilder.CreateType();
}
}
显然我知道如何从Type
创建MakeGenericType
,但在我正在使用的现实场景中,我不想(或者不能)修改类型名称,以便我可以单独创建类型并将它们拼凑在一起。
如何从Type
名称重新创建此类string
?
答案 0 :(得分:0)
我突然意识到我应该使用Assembly.GetType()
版本的错误引发Exception
。它显示了一个FileNotFoundException
,显然,在泛型参数中,只允许来自文件的程序集。为了解决这个问题,我从AssemblyResolve
注册了AppDomain
事件,并处理了在文件中找不到DynamicAssembly
的情况。我的工作代码:
static public class DynamicTypeTest
{
public class Generic<T> { }
static public void Test()
{
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
Type dynamicType = createDynamicType();
Type genericType = typeof(Generic<>);
// Generic<DynamicType>
Type genericDynamicType = genericType.MakeGenericType(new Type[]{dynamicType});
Debug.Assert(TypeFromName(genericDynamicType.FullName) == genericDynamicType); // pass
AppDomain.CurrentDomain.AssemblyResolve -= CurrentDomain_AssemblyResolve;
}
static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
{
AssemblyName assemblyName = assembly.GetName();
if (args.Name == assemblyName.FullName)
{
return assembly;
}
}
return null;
}
// This is the essence of what I want to do
static private Type TypeFromName(string name)
{
try
{
return typeof(Generic<>).Assembly.GetType(name, true);
}
catch (Exception)
{
return null;
}
}
static private Type createDynamicType()
{
AssemblyName assemblyName = new AssemblyName("DynamicAssembly");
AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("DynamicModule");
TypeAttributes typeAttributes = TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.AutoClass | TypeAttributes.AnsiClass;
TypeBuilder typeBuilder = moduleBuilder.DefineType("DynamicType", typeAttributes, typeof(System.Object));
return typeBuilder.CreateType();
}