.net - Extension methods overloading in C#, does it work? -


A class that has a method, like this:

  class window {public It is possible to overload this method more widely:  
  class Window Extensions {Public Zero Display (This Window Window, Object O) {Button Button = Blow Blah (O); Window.Display (button); }}  

What happened when I tried that I have infinite recursion is this a way to do the job? I want the extension method to be called only when the other method can not be called. Let's go for the specification first, we must understand the rules for method invocations.

Roughly, you start with such an example, from the example you are trying to apply a method. You are looking at an accessible method looking for a legacy chain. Then you make your own conclusions and overload resolution rules, and if it succeeds, use the method, then if only such a method is not found, then you try to process the method as an extension method. Therefore, §7.5.5.2 (Extension method invocation), in particular, bold statement:

A method invitation in one form (§7.5.5.1)

expr.identifier ()

expr.identifier (args)

expr.identifier & lt; Typeargs & gt; ()

expr.identifier & lt; Typeargs & gt; (Args)

If the normal process of invocation does not apply, an extension method orientation is attempted in the form of a process. >

In addition, the rules become a bit complicated, but for the simple matter you have presented to us, it is very simple if there is no periodic example method then the extension method window extension. Display (window, object) will be applied. Example method applies if the parameter window Display is a button or Built-in is valid for one button Otherwise, the extension method will be applied (because everything from object Code> object ).

Therefore, unless there is a significant bit that you are getting out, what you are trying to do will work.

Then, consider the following example:

  class button {} square window {public zero display (button button) {Console.WriteLine ("Window.Button") ; }} Class NotAButtonButCanBeCastedToAButton {public fixed underlying operator button (NotAButtonButCanBeCastedToAButton nab) {new button (back); }} Class NotAButtonButMustBeCastedToAButton {public static clear operator button (NotAButtonButMestBeCastedToAButton nab) {new button back (); }} Fixed class window extension {public static zero display (this window window, object o) {Console.WriteLine ("WindowExtensions.Button: {0}", o.ToString ()); Button button = blah blah (o); Window.Display (button); } Public Static Button BlahBlah (Object O) {Return New Button (); }} Class program {static zero main (string [] args) {window w = new window (); Object o = new object (); W.Display (O); // Extension I = 17; W.Display (i); // extension string s = "hello, world!"; W.Display (s); // extension button b = new button (); W.Display (b); // Example var nab = new NotAButtonButCanBeCastedToAButton (); W.Display (b); // Insert cast so example var nabexplict = new NotAButtonButMustBeCastedToAButton (); W.Display (nabexplict); // Only clear cast so expansion w Performance (nabexplict (button); // Extension removed from such example}}  

it will print

  window extension. Button: System. Object window Button window extension. Button: 17 Window. Button WindowExtensions.Button: Hello, world! window. Button Window.Button Window.Button WindowExtensions.Button: NotAButtonButMeCastedToAButton Window.Button Window.Button  

On the console.


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -