I have a generic class as shown below
public class MyClass
{
public T MyProp { get; set; }
}
Now I want to return the instance of
I am not sure if this fits your use, but your only way out is the use of an interface that is covariant.
public interface IMyClass
{
T MyProp { get; }
}
public class MyClass : IMyClass
{
public T MyProp { get; set; }
}
public IMyClass
This code compiles because your return type is not a class, but a covariant interface (note the out T
on the type parameter). That interface allows retrieval only, so the get;set
on the property has been replaced by a get on the interface.