Consider the code:
public interface IGeneral {}
public interface ISpecific : IGeneral {}
public Func<IGeneral, String> Cast(Object specificFuncAsObject) {
var generalFunc = specificFuncAsObject as Func<IGeneral, String>;
Assert.IsNotNull(generalFunc); // <--- casting didn't work
return generalFunc;
}
Func<ISpecific, String> specificFunc = specific => "Hey!";
var generalFunc = Cast(specificFunc);
Is there a way to make such casting work? I know that IGeneral cannot be casted to ISpecific in general case. But in my particular situation I wish I could do something like this:
Func<IGeneral, String> generalFunc = new Func<IGeneral, String>(general => specificFunc(general as ISpecific));
But having the specificFunc
as Object and having ISpecific
type only via specificFuncAsObject.GetType()