c# 3.0 - Forcing the use of a specific overload of a method in C# -
i have overloaded generic method used obtain value of property of object of type pagedata
. properties collection implemented dictionary<string, object>
. method used avoid tedium of checking if property not null
, has value.
a common pattern bind collection of pagedata
repeater. within repeater each pagedata
container.dataitem
of type object
.
i wrote original extension method against pagedata:
public static t getpropertyvalue<t>(this pagedata page, string propertyname);
but when data binding, have cast container.dataitem
pagedata
:
<%# ((pagedata)container.dataitem).getpropertyvalue("someproperty") %>
i got little itch , wondered if couldn't overload method extend object, place method in separate namespace (so not pollute inherits object
) , use namespace in aspx/ascx files know i've databound collection of pagedata
. this, can avoid messy cast in aspx/ascx e.g.
// new overload public static t getpropertyvalue<t>(this object page, string propertyname); // , new usage <%# container.dataitem.getpropertyvalue("someproperty") %>
inside object
version of getpropertyvalue
, cast page
parameter pagedata
public static t getpropertyvalue<t>(this object page, string propertyname) { pagedata data = page pagedata; if (data != null) { return data.getpropertyvalue<t>(propertyname); } else { return default(t); } }
and forward call onto, expect pagedata
version of getpropertyvalue
, however, i'm getting stackoverflowexception
it's re-calling object
version.
how can compiler realise pagedata
overload better match object
overload?
the extension method syntax syntactic sugar call static methods on objects. call other regular static method (casting arguments if necessary).
i.e.,
public static t getpropertyvalue<t>(this object page, string propertyname) { pagedata data = page pagedata; if (data != null) { //will call getpropertyvalue<t>(pagedata,string) overload return getpropertyvalue<t>(data, propertyname); } else { return default(t); } }
[edit]
in light of comment, wrote test program see behavior. looks go local method.
using system; using test.nested; namespace test { namespace nested { public static class helper { public static void method(this int num) { console.writeline("called method : test.nested.helper.method(int)"); } } } static class helper { public static void method(this object obj) { console.writeline("called method : test.helper.method(object)"); } } class program { static void main(string[] args) { int x = 0; x.method(); //calls object overload console.write("press key continue . . . "); console.readkey(true); console.writeline(); } } }
to make sure nesting not affecting anything, tried removing object overload:
using system; using test.nested; namespace test { namespace nested { public static class helper { public static void method(this int num) { console.writeline("called method : test.nested.helper.method(int)"); } } } static class helper { public static void method(this string str) { console.writeline("called method : test.helper.method(string)"); } } class program { static void main(string[] args) { int x = 0; x.method(); //calls int overload console.write("press key continue . . . "); console.readkey(true); console.writeline(); } } }
sure enough, int
overload called.
so think it's that, when using extension method syntax, compiler looks within current namespace first appropriate methods (the "most local"), other visible namespaces.
Comments
Post a Comment