KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
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
Tags (comma-separated)
Save Edits
Cancel