开发者

How to execute a method everytime before/after executing some other method which has many overloads?

I have an existing application which is already developed and is in use.

There are multiple overloads of one of the methods (called Read()) of a class. Now I want to introduce my method in that class which should be executed before or after an开发者_如何学Cy of the overloads of the Read() method is called. Which overload of the Read() method will be called is decided at run time depending on the user input.

What do i need to do in this case? How my method will be executed before or after executing any of the existing overloads of Read() method?


You need to use an Interceptor / AoP. The first options that jump to mind are PostSharp and Microsoft Unity.

There's a very good article on this sort of thing in this months MSDN magazine: http://msdn.microsoft.com/en-us/magazine/gg490353.aspx


Have a generic method

public void Read<T>(T t) where t:ISomeCommonInterface
{
   Before();
   Read(t);
   After();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜