I have an interface :
public interface IHello {}
I have 2 classes :
public class A : IHello
{
public void Method1()
{
...
}
}
public class B : IHello
{
}
When I make a call to Method1(), I get the following error :
public class C
{
private IHello obj123;
static void Main()
{
obj123 = new A();
obj123.Method1(); // Method1 does not exist in IHello
}
}
I understand that obj123 is of type IHello. I want to do something like (GetTypeof(Ihello)).Method1 exists? Then call
How to achieve this?
if (obj123 is A a)
{
a.Method1();
}
Please note that this type of selectively casting is often seen as bad OOP design. You should probably not rely on it too much and instead find a design where this isn't neccessary.