Comparison of basic types in reflection

I call the method on the assembly using reflection, and I need to first compare whether one of the parameters for the method has the same base type with the parameter that I pass for it.

But when I call passedInParameter.GetType().BaseType(), it returns "

{Name = "MarshalByRefObject" FullName = "System.MarshalByRefObject"}.

Should I show the interface that it implements?

+5
source share
2 answers

There are helpers in the runtime for this:

if (typeof(ISomeInterface).IsAssignableFrom(passedInParameter.GetType()))
{
}

Reference:

Interfaces are not basic. CLR types cannot have multiple base types.

, , , ,

+4

. ,

passedInParameter.GetType().GetInterfaces();

is operator

if(passedInParameter is ISomeInterface)
{
    // do some logic
}

    ParameterInfo param = paramList[i]; 
    Type type = paramArray[i].GetType();

    bool valid = false;
    if (info.ParameterType.IsInterface)
        valid = type.GetInterfaces().Contains(param.ParameterType);
    else
        valid = type.IsSubclassOf(param.ParameterType);
+2

All Articles