小编Cra*_*aig的帖子

从抽象类调用GetMethod来获取子类的方法?

我有类似的东西

public abstract class Parent
{
    public void BeginRequest()
    {
        var thisType = this.GetType();
        var methodInfo = thisType.GetMethod("DoSomething", System.Reflection.BindingFlags.FlattenHierarchy);

        Response.Write(methodInfo.Invoke(this, null));
    }
}

public class Child : Parent
{
    public static string DoSomething() {....}
}
Run Code Online (Sandbox Code Playgroud)

问题是methodInfo总是设置为null.如果我在Parent中创建DoSomething()它可以正常工作.我并不感到惊讶它没有正确接线,但有没有办法可以使它工作?

c# reflection

2
推荐指数
1
解决办法
1412
查看次数

标签 统计

c# ×1

reflection ×1