c# - Check if types are castable / subclasses -


I have two members in the form of wire - and not as a typed example. How can I check that there are two types of characters? Let's say a string is "System.Windows.Forms.Label" and the other is "System.Windows.Forms.Control". How can I check that there is a subclass (or underlying castable) of a second class first? Is it possible to use reflection?

Thank you!

It may seem that you should use,

true if c and

Current [example] type represent the same type, or if the current [example] type is inherited in the c hierarchy, or if Current [example] type an interface Is the one which applies c , or if c is a normal type parameter and current [example] type c Indicates one of the obstacles. false If none of these conditions are true , or if c is a null reference ( Nothing <
  class base {} clause notabase {public stable vendor operator base (NotABase o) {/ console.wrightline (typef.org) .IsAssignableFrom (typeof (NotABase) );;   

will print incorrect on the console, even if NotABase s is included on the base s Therefore, to handle casting, we can use reflection in this way:

  Fixed class type extension {public static bool isCastableTo (by type, type) {if (to.IsAssignableFrom (from)) {return true;} Var methods = from.GetMethods (BindingFlags.Public | BindingFlags. Static). Where (M => m.ReturnType == C & amp; amp; amp; amp; amp; amp; amp; name == "op_Implicit" || m.Name == "Op_Explicit")); Return methods Calculate ()> 0; }}  

Usage:

  console WrightLine (typef (string) .IsCastableTo (typeof (int)); // false console.light line (notabase) .IsCastableTo (typefile)); // true  

and for your case

 represents the string name type from  //, e.g. "System.Windows.Forms.Label" // is the name representing the string name, e.g. Type "Type from System.Windows.Forms.Control" type = type. GetType (from); Type = type to type. Gettype (copy); Bool castable = from.IsCastableTo (to);  

Comments

Popular posts from this blog

php - multilevel menu with multilevel array -

c# - TypeConverter in propertygrid only converts from string, not to -

jQuery UI: Datepicker month format -