How do I know if an interface is derived from a specific interface?

I have an interface like this:

 public interface IViewA : IViewB, IViewC
 {
    byte prop { get; set; }
 }

and I have a general method:

public void OpenPopup<T>(WindowState state)
{
    if ((typeof(T) as IViewC)!=null)
    {
         //Process A
    }
    else
    {
        //Process B
    }

}

Although I am sending T as an interface that comes from IViewC, process A is not processed.

So, how to learn at runtime through reflection, does an interface flow from another interface?

thank

+3
source share
5 answers

Try to execute

if ( typeof(IViewC).IsAssignableFrom(typeof(T)) { 
  ...
}
+4
source

typeofUse instead isAssignableFrom.

+2
source

- typeof(IViewC).IsAssignableFrom(typeof(T)).

typeof(T), System.Type IViewC, .

+2

typeof(T) as IViewC . Type IViewC, , null.

typeof(T).GetInterfaces() , , .

+1
if ((typeof(T) as IViewC)!=null)

It is not right. What you wrote checks to see if the object Typereturned typeof(T)is IViewC, which is obviously not.

Do you want to:

if (typeof(IViewC).IsAssignableFrom(typeof(T))
0
source

Source: https://habr.com/ru/post/1755803/


All Articles