This doesn't seem to work. Is it possible?
public interface IInterface1 {}
public interface IInterface2 { }
public interface IMyT1 : IInterface1 {}
public interface IMyT2 : IInterface2 {}
public abstract class BaseClass<T1, T2> : Interface2 where T1 : IInterface1, where T2 : IInterface2
{
protected T1 T1Obj {get;set;}
public static explicit operator T1(BaseClass<T1,T2> val)
{
return val.T1Obj;
}
}
public class SomeClass : BaseClass<IMyT1, IMyT2>, ISomeClass {}
Any casts to SomeClass fail with an InvalidCastException
.
ISomeClass mySomeClass = new SomeClass();
var myT1 = (IMyT1)mySomeClass; //Boom!
It may be crazy but is it possible?