How to execute a method everytime before/after exe

2019-02-14 10:50发布

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 any 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?

标签: c# .net-2.0
2条回答
女痞
2楼-- · 2019-02-14 11:22

Have a generic method

public void Read<T>(T t) where t:ISomeCommonInterface
{
   Before();
   Read(t);
   After();
}
查看更多
倾城 Initia
3楼-- · 2019-02-14 11:46

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

查看更多
登录 后发表回答