c# - Comparison of unspecified generic properties -


consider following code:

public interface iidentifiable<t> {     t id { get; set; } }  public interface iviewmodel { }  public class myviewmodel1 : iviewmodel, iidentifiable<int> {     public string myproperty { get; set; }      public int id { get; set; } }  public class myviewmodel2 : iviewmodel, iidentifiable<string> {     public string myproperty { get; set; }      public string id { get; set; } } 

i have class operates viewmodels:

public class loader<t> t: iviewmodel {     public void loaddata()     {         /*some important stuff here*/          if (typeof(iidentifiable<??>).isassignablefrom(typeof(t)))         {                     // ^- here's first problem             data = data.where(d => _datasource.all(ds => ((iidentifiable<??>) ds).id != ((iidentifiable<??>) d).id)).tolist();         }                                                             // ^---- , there second ----^          /*some important stuff here too*/     } } 

now, can see, viewmodels have might implement iidentifiable<> interface. want check that, , if it's true, want make sure data list not contains entry present in _datasourse list.

so have 2 questions:

  1. i don't know iidentifiable<> has in generic parentheses, might int, string or guid. tried typeof(iidentifiable<>).isassignablefrom(typeof(t)) correct syntax, yet returns false. there way check whether t iidentifiable<> without knowing exact generic type?

  2. if there answer first question, know how can compare id fields without knowing type. found this answer quite useful, yet doesn't cover specific case.

i know can solve problem if make loader<t> class generic 2 types loader<t,k>, k type in iidentifiable<>, yet know if there other solutions.


p.s. in addition first question: i'm curious why 1 can write typeof(iidentifiable<>).isassignablefrom(typeof(t)) if returns false when generic type of iidentifiable<> not specified?


edit: guess, in hindsight, understand why can't write code bluntly - because there's might collection icollection<iviewmodel> entries implement different types of iidentifiable<> (or don't implement @ all), , check fail awkwardly. yet maybe there way restrictions, without creating second generic parameter loader?

try add 2 methods loader<t>:

public bool cancast<tid>() {     var identifiablet = typeof(iidentifiable<>).makegenerictype(typeof(tid));     return identifiablet.isassignablefrom(typeof(t)); }  public ienumerable<iidentifiable<tid>> filter<tid>(ienumerable<t> data) {     return data.where(d => _datasource.all(       ds => !((iidentifiable<tid>) ds).id.equals(((iidentifiable<tid>) d).id))); } 

then in loaddata

if (cancast<int>())     data = filter<int>(data); else if (cancast<guid>())     data = filter<guid>(data); // , om 

Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -