c# - Determine if a derived class implements a generic interface with itself as the generic parameter -


i have seen answers on similar questions did not find 1 addressing criteria below.

how can determine whether class b meets following criteria when b inherits a:

  • [b] not implement [additional] interfaces (generic or not).
  • [a] implements generic interface own type generic parameter?

the following

object o = new someobject(); bool result = (o.gettype().getinterfaces()[0] == typeof(iknowninterface<???>)); // ??? should typeof(o). how achieve this? 

i know can interface name string type "namespace.classname+iknowninterface'1[[sometype, modulename, version=1.0.0.0, culture=neutral, publickeytoken=null]]" not seem intuitive or safe. plus '1 notation incremental based on number of generic types used interface.

i either going wrong way or missing silly here. please advise.

this should trick

//get 2 types in question var typeb = b.gettype() var typea = typeb.basetype; var interfaces = typea.getinterfaces();  //if length different b implements 1 or more interfaces not if(typeb.getinterfaces().length != interfaces.length){    return false; }   //if list non-empty @ least 1 implemented interface satisfy conditions return (from inter in interfaces         //should generic         inter.isgeneric         let typedef = inter.getgenerictypedefinition()         //the generic type of interface should specific generic type         typedef == typeof(iknowninterface<>) &&         //check whether or not type 1 of type arguments               inter.getgenerictypearguments.contains(typea)).any() 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -