c# - Is it possible to restrict generic overload to property type? -
the question can somehow restrict generic overload strictly property type.. not clear now, here example:
i have class
public class obj { public double width { get; set; } public float widthf { get; set; } }
and extension it
public static class testgenericoverride { public static void do<tobj, tprop>(this tobj src, expression<func<tobj, tprop>> expr, tprop val) { console.writeline("do: typeof(tprop) = {0}", typeof(tprop).name); } }
and main
static void main(string[] args) { var o = new obj(); o.do(p => p.width, 100); //1: typeof(tprop) = double o.do(p => p.width, 100.0); //2: typeof(tprop) = double o.do(p => p.widthf, 100); //3: typeof(tprop) = single o.do(p => p.widthf, 100.9); //4: typeof(tprop) = double console.readline(); }
what want make last call (#4) unavailable. possible?
in other words: if type of property in expression float, want float-overload available, not double-overload
thank you!
this job:
public static void do<tobj, tprop, tvalue>(this tobj src, expression<func<tobj, tprop>> expr, tvalue val) tvalue: tprop { console.writeline("do: typeof(tprop) = {0}", typeof(tprop).name); }
new value needs same type property. means not #4 doesn't work #1 , #3 because , int not float. implicit conversion not allowed using extension.
Comments
Post a Comment