c# - object - How top most base class got Method. [Extension Method] -


yesterday gone through article eventaggregator, there shot of code written this,

(message.text object).publishevent(publisheventnames.messagetextchanged);   public static class extensionservices     {         //supplying event broking mechanizm each object in application.         public static void publishevent<teventsubject>(this teventsubject eventargs, string eventtopic)         {             servicesfactory.eventservice.getevent<genericevent<teventsubject>>()                 .publish(new eventparameters<teventsubject> { topic = eventtopic, value = eventargs });         }     } 

my question is, how object got method "publishevent". oop understanding wrong?

it implemented extension method on object class.

for example, extension method (from linked article):

public static class myextensions {     public static int wordcount(this string str)     {         return str.split(new char[] { ' ', '.', '?' },                           stringsplitoptions.removeemptyentries).length;     } }    

is defined on string class (by using this string syntax , static method on static class) .

in project defined in string has wordcount method (so long in correct namespace).


Comments