Moh*_*vel 2 c# oop prism c#-4.0
昨天我浏览了一些关于EventAggregator的文章,有一些像这样编写的代码,
(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 });
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,对象如何获得方法"PublishEvent".我的OOP理解是错的吗?
它是作为类的扩展方法实现的object
.
例如,这个扩展方法(来自链接文章):
public static class MyExtensions
{
public static int WordCount(this String str)
{
return str.Split(new char[] { ' ', '.', '?' },
StringSplitOptions.RemoveEmptyEntries).Length;
}
}
Run Code Online (Sandbox Code Playgroud)
在String
类上定义(通过this String
在静态类上使用语法和静态方法).
在String
现在定义的项目中有一个WordCount
方法(只要它也在正确的命名空间中).