I have a base class, with a method, where I would like to use generics to force the coder to use a generic expression on the current class:
public class TestClass
{
public void DoStuffWithFuncRef<T>(Expression<Func<T, object>> expression) where T : TestClass
{
this.DoStuffWithFuncRef(Property<T>.NameFor(expression));
}
}
Now, I would like to force T to be of the type of the instantiated class, which I hope will the cause the C# compiler to automatically understand the generic type to use. E.g. I would like to avoid coding the doStuff method below, where I have to specify the correct type - but rather use the doStuffILikeButCannotGetToWork method:
public class SubTestClass : TestClass
{
public string AProperty { get; set; }
public void doStuff()
{
this.DoStuffWithFuncRef<SubTestClass>(e => e.AProperty);
}
public void doStuffILikeButCannotGetToWork()
{
this.DoStuffWithFuncRef(e => e.AProperty);
}
}
Is this possible? Should I be doing this in a different way?