c# - Generic extension method with custom return type -


i trying write extension method 2 entities.

first find type of object, inner join table.

if it's type join must b. if it's type b join a. got stuck on join condition.

public static c getallinfo<t>(this iqueryable<t> objcust) {     if (typeof(t) == typeof(b))     {         //prepare object based on type          var objcastreg = objcust iqueryable<b>;         //how write join here   ?????         var  objusermaster=objcastreg.groupjoin(a,um=>um.userid,r=>r.)         //build class object  2 retrieved objects.     }     if (typeof(t) == typeof(a))     {         var objcast = objcust iqueryable<a>;     }     return null; }  public class c {     public a{ get; set; }      public b b{ get; set; } } 

sounds shouldn't use generics @ all. generics when generic method does not need know type. generic type parameter signals method can work concrete type whatsoever.

maybe should have 2 special-cased method both cases here. makes casting , complexity go away.

but if insist on generic method, here how it. first create special-case method speaking of.

public static c getallinfo(this iqueryable<a> objcust); //implement public static c getallinfo(this iqueryable<b> objcust); //implement 

then delegate them:

public static c getallinfo<t>(this iqueryable<t> objcust) {     if (typeof(t) == typeof(b))     {         return getallinfo((iqueryable<b>)objcust); //call specialized impl.     }     if (typeof(t) == typeof(a))     {         return getallinfo((iqueryable<a>)objcust); //call specialized impl.     }     //fail } 

Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -