Alex Rivera | Logout

Check if types are castable / subclasses

Asked 2010-01-22T18:15:38.527
20

I have they type of two members as strings - and not as a Type instance. How can I check if the two types are castable? Let's say string one is "System.Windows.Forms.Label" and the other one is "System.Windows.Forms.Control". How can I check if the first one is a subclass (or implicit castable) of the second one? Is this possible by using reflection?

Thanks for you support!

c#
Edit
Report

1 Answer

4

How about:

public bool IsCastable(String type0, String type1)
{
  return Type.GetType(type1).IsAssignableFrom(Type.GetType(type0));
}
answered 2010-01-22T18:20:14.243

Your Answer