问题描述
是否有可能在C#中在运行时创建一个继承自基类的模板参数是当前正在构造的类的泛型类的类型?这将编译得很好:
//我有这个类:
public class OtherClass< T>
其中T:OtherClass< T>
{}
//我想在运行时创建它:
public class MyClass:OtherClass< MyClass>
{}
但我不确定如何创建 MyClass
使用 System.Reflection.Emit.ModuleBuilder.TypeBuilder
:
AssemblyName asn = new AssemblyName(test.dll);
AssemblyBuilder asb = AppDomain.CurrentDomain.DefineDynamicAssembly($ b $ asn,AssemblyBuilderAccess.RunAndSave,@D:\test_assemblies);
ModuleBuilder = modb = asb.DefineDynamicModule(test,test.dll);
TypeBuilder tb = modb.DefineType(
test,
TypeAttributes.Public | TypeAttributes.Class,
typeof(MyClass)); //如何指定继承?
//填充属性,字段,方法等等,发出...
tb.CreateType();
这可能吗?
编辑 - 根据到目前为止的回复,我已经试过了:
public class OtherClass< T>
其中T:OtherClass< T>
{}
public static void CreateSimple()
{
AssemblyName asn = new AssemblyName(test);
AssemblyBuilder asb = AppDomain.CurrentDomain.DefineDynamicAssembly($ b $ asn,AssemblyBuilderAccess.RunAndSave,@D:\test_asms);
ModuleBuilder modb = asb.DefineDynamicModule(test,test.dll);
尝试
{
TypeBuilder tb = modb.DefineType(
MyClass,
TypeAttributes.Public | TypeAttributes.Class);
键入my = tb.CreateType();
tb.SetParent(typeof(OtherClass<>)。MakeGenericType(my));
my = tb.CreateType();
}
catch(异常e)
{
throw;
$ / code>
但是得到这个例外:
$ b 'MyProject.Factory + OtherClass`1上的$ b
GenericArguments [0],'MyClass'1 [T]'
违反了' T。
编辑:这是我最后的工作答案: p>
AssemblyName asn = new AssemblyName(test.dll);
AssemblyBuilder asb = AppDomain.CurrentDomain.DefineDynamicAssembly($ b $ asn,AssemblyBuilderAccess.RunAndSave,@D:\test_assemblies);
ModuleBuilder modb = asb.DefineDynamicModule(test,test.dll);
TypeBuilder tb = modb.DefineType(
test,
TypeAttributes.Public | TypeAttributes.Class);
// Typebuilder是Type
的子类tb.SetParent(typeof(OtherClass<>)。MakeGenericType(tb));
var t2 = tb.CreateType();
var i = Activator.CreateInstance(t2);
诀窍是使用参数化的泛型类型调用SetParent,参数是类型的类型构建器构建自己。
使用方法。
使用它时要小心,异常抛出延迟到 CreateType
调用:
构建泛型类型 OtherClass< T> ;
,使用方法:
var genericType = typeof(OtherClass<>)。MakeGenericType(typeof我的课));
Is it possible in C# to create a type at runtime that inherits from a generic class where the template parameter for the base class is the current class being constructed? This will compile fine:
// I have this class:
public class OtherClass<T>
where T : OtherClass<T>
{ }
// I want to create this at runtime:
public class MyClass : OtherClass<MyClass>
{ }
but I'm not sure how to create the MyClass
using System.Reflection.Emit.ModuleBuilder.TypeBuilder
:
AssemblyName asn = new AssemblyName("test.dll");
AssemblyBuilder asb = AppDomain.CurrentDomain.DefineDynamicAssembly(
asn, AssemblyBuilderAccess.RunAndSave, @"D:\test_assemblies");
ModuleBuilder = modb = asb.DefineDynamicModule("test", "test.dll");
TypeBuilder tb = modb.DefineType(
"test",
TypeAttributes.Public | TypeAttributes.Class,
typeof(MyClass)); // How to specify inheritance?
// populate properties, fields, methods, etc., emit...
tb.CreateType();
Is this possible?
Edit - Based on replies so far, I've tried this:
public class OtherClass<T>
where T : OtherClass<T>
{ }
public static void CreateSimple()
{
AssemblyName asn = new AssemblyName("test");
AssemblyBuilder asb = AppDomain.CurrentDomain.DefineDynamicAssembly(
asn, AssemblyBuilderAccess.RunAndSave, @"D:\test_asms");
ModuleBuilder modb = asb.DefineDynamicModule("test", "test.dll");
try
{
TypeBuilder tb = modb.DefineType(
"MyClass",
TypeAttributes.Public | TypeAttributes.Class);
Type my = tb.CreateType();
tb.SetParent(typeof(OtherClass<>).MakeGenericType(my));
my = tb.CreateType();
}
catch (Exception e)
{
throw;
}
}
but get this exception:
GenericArguments[0], 'MyClass', on 'MyProject.Factory+OtherClass`1[T]'
violates the constraint of type 'T'.
Edit : Here is my final working answer :
AssemblyName asn = new AssemblyName("test.dll");
AssemblyBuilder asb = AppDomain.CurrentDomain.DefineDynamicAssembly(
asn, AssemblyBuilderAccess.RunAndSave, @"D:\test_assemblies");
ModuleBuilder modb = asb.DefineDynamicModule("test", "test.dll");
TypeBuilder tb = modb.DefineType(
"test",
TypeAttributes.Public | TypeAttributes.Class);
// Typebuilder is a sub class of Type
tb.SetParent(typeof(OtherClass<>).MakeGenericType(tb));
var t2 = tb.CreateType();
var i = Activator.CreateInstance(t2);
The trick is to call SetParent with a parametrised generic type, the parameter is the typebuilder of the type being constructed itself.
Use the TypeBuilder.SetParent(Type parent) method.
Be careful when using it, exception throwing is deferred to CreateType
call :
To build your generic type OtherClass<T>
, use the MakeGenericType method :
var genericType = typeof(OtherClass<>).MakeGenericType(typeof(MyClass));
这篇关于创建类型“MyClass:OtherClass< MyClass> {}“在运行时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!