简体   繁体   English

C#反射-GetMethod

[英]C# Reflection - GetMethod

I'm working on a project where System.Web.Helpers.Webgrid is being used as a base class for a new grid. 我正在一个项目中,其中System.Web.Helpers.Webgrid被用作新网格的基类。 I believe that it uses reflection to invoke private methods of the base class. 我相信它使用反射来调用基类的私有方法。

Could someone please clarify....... In the code below, seeing as GetDefaultColumnsMethod returns something, can I presume that System.Web.Helpers.WebGrid has a method on it named "GetDefaultColumns" ? 有人可以澄清一下。......在下面的代码中,看到GetDefaultColumnsMethod返回了一些信息,我可以假定System.Web.Helpers.WebGrid有一个名为"GetDefaultColumns"吗?

private MethodInfo GetDefaultColumnsMethod 
{
    get { return typeof(System.Web.Helpers.WebGrid).GetMethod("GetDefaultColumns", BindingFlags.Instance | BindingFlags.NonPublic); }
}

private IEnumerable<WebGridColumn> GetDefaultColumns(IEnumerable<string> exclusions)
{
    return (IEnumerable <WebGridColumn>)GetDefaultColumnsMethod.Invoke((System.Web.Helpers.WebGrid)this, BindingFlags.Instance | BindingFlags.NonPublic, null, new object[] { exclusions }, null);
}

In the code below, seeing as GetDefaultColumnsMethod returns something, can I presume that System.Web.Helpers.WebGrid has a method on it named "GetDefaultColumns"? 在下面的代码中,看到GetDefaultColumnsMethod返回了某些内容,我可以假定System.Web.Helpers.WebGrid上有一个名为“ GetDefaultColumns”的方法吗?

Yes. 是。

  • Type.GetMethod searches for the method specified by its name Type.GetMethod搜索由其名称指定的方法
  • BindingFlags.Instance | BindingFlags.NonPublic BindingFlags.Instance | BindingFlags.NonPublic means you search an instance method (as opposed to a static one) that is not public (do note it might me protected / internal though). BindingFlags.Instance | BindingFlags.NonPublic意味着您搜索的实例方法(相对于static方法)是非public (请注意,它可能是protected / internal )。

Digging deeper - i added the debug code below and I now have a list of the methods.... 深入研究-我在下面添加了调试代码,现在有了方法列表。

So I can now debug and see what is there.....and more to the point, what is not in newer versions of the WebGrid component. 因此,我现在可以调试并查看其中的内容……,更重要的是,WebGrid组件的较新版本中没有。

    Type tt = this.GetType();
    Type t = tt.BaseType;

    PropertyInfo[] props =
        t.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
    MethodInfo[] mths = t.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM