c# - Understanding this function that returns Type object -


i got cool method here check if type derived another. while refactoring code got chunk getblah.

public static bool isof(this type child, type parent) {     var currentchild = child.getblah(parent);      while (currentchild != typeof(object))     {         if (parent == currentchild)             return true;          if(currentchild.getinterfaces().any(i => i.getblah(parent) == parent))             return true;          if (currentchild.basetype == null)             return false;          currentchild = currentchild.basetype.getblah(parent);     }      return false; }  static type getblah(this type child, type parent) {     return child.isgenerictype && parent.isgenerictypedefinition           ? child.getgenerictypedefinition()           : child; } 

i'm having trouble understanding getblah , hence can't give proper name. mean can understand ternary expression such , getgenerictypedefinition function, dont seem use in isof method, parent argument being passed. can elucidate getblah method returning?

bonus: suggest me apt name method :)

generic types list<int> or list<string>. make use of same generic type definition: list<>.

isgenerictype return true type generic type. if type generic type definition isgenerictypedefinition should return true. function getgenerictypedefinition return generic type definition of generic type.

so, if do:

typeof(list<int>).getgenerictypedefinition(); 

you typeof(list<>).

so far theory!

if analyse code correctly, return true of child derived parent. made little checklist of type combination should return true (in opinion):

a: int, icomparable<int> b: int, valuetype c: int, object d: list<int>, ilist<int> e: list<int>, ienumerable<int> f: list<int>, object  g: list<int>, list<> h: list<int>, ilist<> i: list<>, ilist<> j: list<>, object 

the given code fails @ 1 point: every time when parent of type object, false returned. resolved modifying while-condition into:

while (currentchild != null) 

now blah-function. check if parent generic type definition. no 'normal' class (generic or not) can derive generic type definition. generic type definition can derive generic type definition. in order let case g , h become true, special converion has done. if parent generic type definition , when child can converted generic type definition, child converted generic type defintion.

that does.

so perfect name function be: convertchildtogenerictypedefinitionifparentisagenerictypedefinitionandthechildisagenerictype(...)

:)


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -