代码之家  ›  专栏  ›  技术社区  ›  theburningmonk

如何在C中找到System.Reflection.MethodBase方法的返回类型?

  •  24
  • theburningmonk  · 技术社区  · 15 年前

    如何从methodbase中找出方法的返回类型?我正在使用postsharp并尝试重写compileTimevalidate(methodBase方法)方法,以确保将属性应用于具有正确签名的方法。

    谢谢,

    4 回复  |  直到 10 年前
        1
  •  18
  •   JoshBerke    15 年前

    methodBase用作 MethodInfo 有财产的 ReturnType .

    您可以尝试强制转换到MethodInfo的实例并检查该属性。

        2
  •  21
  •   JaredPar    15 年前

    MethodBase 本身没有返回类型,因为除了普通方法之外,它还用于表示没有返回类型的方法,例如构造函数。相反,你需要看看它是否是 MethodInfo 检查一下那个 ReturnType 财产。

    CompileTimeValidate(MethodBase method) {
      var normalMethod = method as MethodInfo;
      if( normalMethod != null) {
        ValidateReturnType(normalMethod.ReturnType);
      }
    }
    
        3
  •  1
  •   Alex    10 年前

    试试这个。 MethodInfo 拥有财产,但是 MethodBase 也用于构造函数,它们没有返回类型。

    MethodBase b = this.GetType().GetMethods().First(); 
    if(b is MethodInfo)
        MessageBox.Show((b as MethodInfo).ReturnType.Name);
    
        4
  •  0
  •   Eric Eijkelenboom    15 年前

    试试 MethodInfo.ReturnType 财产。

    要获取返回类型属性,请首先获取 Type . 从 类型 得到 MethodInfo . 从 方法信息 得到 ReturnType .

    似乎你不能用methodbase来完成它…

    http://msdn.microsoft.com/en-us/library/system.reflection.methodinfo.returntype.aspx

    推荐文章