开发者

Dynamic method invocation

I have two simple classes:

public class A
{
    public void DoSomething();
}

public class Listener
{
    public void OnDoSomethingCalled();
}

I want Listener.OnDoSomethingCalled() to be called automatically everytime A.DoSomething() is called. I want to do this without changing class A. I don't want to add a delegate in A and attach Listener to that delegate. Imagine the scenario where I don't have the source code for class A.

I can't use decoration pattern here because I can't modify the code that calls A.DoSomething(). I read something about Reflection.Emit or DynamicMethod to dynamically change or define a method at runtime. Can it be a开发者_JAVA百科pplied here and how?


You could try Aspect Oriented Programming with something like PostSharp, which I believe should handle this (it rewrites the CIL).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜