I’m using a third party library class that has the following (abbreviated) method:
public void DoSomethingAsync(Action<ResultInfo> callback)
Rather than have a reference to this third party library in my application code project, I’ve created a wrapper class that abstracts the third party dependencies. So, what I’m trying to do is have a wrapper method that looks like the following:
public void MyDoSomethingAsync(Action<MyResultInfo> callback)
{
this.wrappedClass.DoSomethingAsync(callback);
}
The problem is that I need to convert the callback parameter from Action<MyResultInfo> to Action<ResultInfo>. Is this possible with a custom implicit cast operator or is there an alternative approach that someone can recommend?
No, you wouldn’t be able to do so directly, even if
MyResultInfoderived fromResultInfo; delegate contravariance wouldn’t apply here as theAction<MyResultInfo>wouldn’t be able to handle any instance derived fromResultInfo(so that disqualifies anAction<MyResultInfo>from being used in place of anAction<ResultInfo>)However, that doesn’t mean you can’t use an adapter, like so: