lundi 1 juillet 2019

IsAssignableFrom - which type argument goes where? Which is first, which is second?

Every time I use IsAssignableFrom to see if a type implements an interface or inherits from a base type, I forget which argument goes where. The documentation is long. I always end up writing a unit test.

Given these types:

public interface IDoesItImplementThis {}

public class DoesItInheritFromThis { }

public class MightInheritOrImplement { }

Which is the correct way to see if an instance of ThingThatMightInheritOrImplement implements the interface or inherits from the base class?

Is it this:

var implements = 
    typeof(IDoesItImplementThis).IsAssignableFrom(typeof(MightInheritOrImplement));

var inherits = 
    typeof(DoesItInheritFromThis).IsAssignableFrom(typeof(MightInheritOrImplement));

or the other way around?

I try to read it as "something is assignable from something" but it doesn't help me remember. I assign things to things. I don't assign things from other things.





Aucun commentaire:

Enregistrer un commentaire