object - How top most base class got Method.

Yesterday i gone through some article about EventAggregator, there some shot of code written like this,

(Message.Text as object).PublishEvent(PublishEventNames.MessageTextChanged);

 public static class ExtensionServices
    {
        //Supplying event broking mechanizm to each object in the 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 the object got the method "PublishEvent". Is my OOP understanding is wrong?

Answers


It was implemented as an Extension Method on the object class.

For example, this extension method (from the linked article):

public static class MyExtensions
{
    public static int WordCount(this String str)
    {
        return str.Split(new char[] { ' ', '.', '?' }, 
                         StringSplitOptions.RemoveEmptyEntries).Length;
    }
}   

Is defined on the String class (by using the this String syntax and a static method on a static class) .

In the project that this is defined in String now has a WordCount method (so long as it is also in the correct namespace).


Need Your Help

Multiple Groups in geom_density() plot

r ggplot2 density-plot

I am trying to plot 3 groups in one geom_density()plot.

NSData to NSString using initWithBytes:length:encoding

iphone nsstring uiimage nsdata

I have some image data (jpeg) I want to send from my iPhone app to my webservice. In order to do this, I'm using the NSData from the image and converting it into a string which will be placed in my...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.