Let’s say you define some arbitrary interface:
public interface IInterface {
void SomeMethod();
}
And let’s say there are some classes that happen to have a matching public interface, even though they do not “implement IInterface“. IE:
public class SomeClass {
public void SomeMethod() {
// some code
}
}
Is there nevertheless a way to get an IInterface reference to a SomeClass instance? IE:
SomeClass myInstance = new SomeClass();
IInterface myInterfaceReference = (IInterface)myInstance;
No there is no way to do this. If the type doesn’t implement the interface then there is no way to cast to it. The best way to achieve behavior similar to the one you want is to create a wrapper type which provides an implementation of
IInterfaceforSomeClass.Then you can make the following call