使用反射来发现所有的公共虚拟方法,并提供一个覆盖 [英] Use reflection to find all public virtual methods and provide an override

查看:128
本文介绍了使用反射来发现所有的公共虚拟方法,并提供一个覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我希望能够跨类的实例进行迭代,发现标记为公共虚拟的所有方法的项目。然后,我要重写的类的实例,以便当调用该方法我可以调用一组不同的code。我知道如何找到所有的市民使用反射一类的方法,但我无法弄清楚如何重写虚拟方法。

基本上,我给一个代理对象来使用,当他们调用该方法,我想调用底层对象的方法。我可以通过手动覆盖每个方法做到这一点,但我想用一些更动感。

解决方案

 的typeof(MyClass的)
    .GetMethods(BindingFlags.Public | BindingFlags.Instance)
    。凡(米=> m.IsVirtual);
 

I have a project where I want to be able to iterate across an instance of a class and find all methods that are marked public virtual. Then I want to override the instance of the class so that when the method is called I can call a different set of code. I know how to find all methods that are public in a class using reflection, but I cannot figure out how to override virtual methods.

Basically I am giving a proxy object to use, and when they call the method, I want to call a method on the underlying object. I can do this by manually overriding each and every method, but I would like to use something a bit more dynamic.

解决方案

typeof(MyClass)
    .GetMethods(BindingFlags.Public | BindingFlags.Instance)
    .Where(m => m.IsVirtual);

这篇关于使用反射来发现所有的公共虚拟方法,并提供一个覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆