c# - How to simplify the call of a generic method, I created to convert from one type to another type across the different layer of my project -
i want sec calling style should similar first calling style. please see details in code. help appreciated.
here code using.
// calling style of first method
var source = db.thinkfeeds.single(tf => tf.id == 1); var target = new myproduct.uientities.thinkfeed(); typeconverter.convertbltoui(source, target);
//calling style of sec method. weird calling style.
var source = myproduct.uientities.book.getbookbyid(1); var target = new myproduct.dtoentities.book(); typeconverter.convertuitodto<myproduct.uientities.book, myproduct.dtoentities.book, book>(source, target);
//first method
public static void convertbltoui<tbl, tui>(tbl entitysource, tui entitytarget) { }
//second method
public static void convertuitodto<tui, tdto, tentity>(tui uientity, tdto dtoentity) tdto : dtoentities.myproductdto<tentity, tdto> tentity : entityobject { }
you can't. generic methods, type inference:
the compiler can infer type parameters based on method arguments pass in; cannot infer type parameters constraint or homecoming value. hence ...
the first style you've shown using type inference, it's convenient shorthand. there times (as here) type inference cannot work, , have supply type parameters explicitly.
c# generics
No comments:
Post a Comment