I'm trying to write some code generating a type at runtime. I have an interface I need to implement but the constraint is causing me some difficulties.
As has been noted in the comments, yes the interface looks like and endless recursion but is not. It compiles just fine
The interface looks similar to:
interface IFoo<T> where T : IFoo<T>{ T MyProperty{get;} } When I the try to define my dynamic type using ModuleBuilder I have an issue:
TypeBuilder tb = mb.DefineType( "typename", TypeAttributes.Public,typeof(object),new[]{...}); have do I get to pass in a IFoo where T is the type I'm trying to define?
The code above in C# but answers in F# that let's me dynamically construct class SomeType : IFoo<SomeType> will do just fine as well
Answer that uses a base type instead of an interface is also valid (as the title suggests). I.e.
TypeBuilder tb = mb.DefineType( "typename", TypeAttributes.Public,...,null); where .. is of SomeType<T> and T is the type being defined
EDIT: As an example of this when written in code could be:
public interface ISelf<T> where T : ISelf<T> { T Prop { get; } } public class SelfBase<T> : ISelf<T> where T : SelfBase<T>{ public T Prop { get { return (T)this; } } } public class FooBar : SelfBase<FooBar>{ public void Bar(){ Prop.NonInterfaceMethod(); } public void NonInterfaceMethod(){} } That piece of code does indeed compile.
ModuleBuilderandTypeBuilderclasses as their API needs aTypefor the interface to implement and you don't have one until you callCreateTypeon the type builder at which point it is too late...