c# - Using type of extension parameter to pass as an Generic type parameter -


i attempting write , extension method takes type of parameter , passes along generically typed method ie

public tdestination methodname<tsource, tdestination>(this tsource obj) {       return destinationclass.destinationmethod<tsource, tdestination>(obj) } 

how can assume type of parameter type of destination method possible defining tsource when calling method

clarification: want create helper method can directly call methodname object , use prewritten generic method 2 types want shorten single destination type , have method assume type of object being passed extension method

usage example: ie automapper extension example

original method call:

  var person = _db.people.single(q=>q.id == 5);   //use automapper directly map object    return mapper.map<person, persondisplay>(person); 

i want shorten down

  var person = _db.people.single(q=>q.id == 5);    //use extension method map    return person.mapto<persondisplay>()   

want shorten

you cannot. generic parameters must explicit or implicit. cannot compiler infer 1 yet supply other. consequently require different api. maybe via intermediate have like:

obj.foo().bar<anothertype>(); 

which possible.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -