当前位置: 首页>>代码示例>>C#>>正文


C# MethodInfo.GetBaseDefinition方法代码示例

本文整理汇总了C#中System.Reflection.MethodInfo.GetBaseDefinition方法的典型用法代码示例。如果您正苦于以下问题:C# MethodInfo.GetBaseDefinition方法的具体用法?C# MethodInfo.GetBaseDefinition怎么用?C# MethodInfo.GetBaseDefinition使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。


在下文中一共展示了MethodInfo.GetBaseDefinition方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: InterfaceImpl

//引入命名空间
using System;
using System.Reflection;

interface Interf
{
   string InterfaceImpl(int n);
}

public class BaseClass
{
   public override string ToString()
   {
      return "Base";
   }

   public virtual void Method1()
   {
      Console.WriteLine("Method1");
   }

   public virtual void Method2()
   {
      Console.WriteLine("Method2");
   }

   public virtual void Method3()
   {
      Console.WriteLine("Method3");
   }
}

public class DerivedClass : BaseClass, Interf
{
   public string InterfaceImpl(int n)
   {
      return n.ToString("N");
   }

   public override void Method2()
   {
      Console.WriteLine("Derived.Method2");
   }

   public new void Method3()
   {
      Console.WriteLine("Derived.Method3");
   }
}

public class Example
{
   public static void Main()
   {
      Type t = typeof(DerivedClass);
      MethodInfo m, mb;
      string[] methodNames = { "ToString", "Equals", "InterfaceImpl",
                               "Method1", "Method2", "Method3" };

      foreach (var methodName in methodNames) {
         m = t.GetMethod(methodName);
         mb = m.GetBaseDefinition();
         Console.WriteLine("{0}.{1} --> {2}.{3}", m.ReflectedType.Name,
                           m.Name, mb.ReflectedType.Name, mb.Name);
      }
   }
}
开发者ID:.NET开发者,项目名称:System.Reflection,代码行数:67,代码来源:MethodInfo.GetBaseDefinition

输出:

DerivedClass.ToString --> Object.ToString
DerivedClass.Equals --> Object.Equals
DerivedClass.InterfaceImpl --> DerivedClass.InterfaceImpl
DerivedClass.Method1 --> BaseClass.Method1
DerivedClass.Method2 --> BaseClass.Method2
DerivedClass.Method3 --> DerivedClass.Method3

示例2: IsOverride

//引入命名空间
using System;
using System.Reflection;

public class ReflectionUtilities
{   
   public static bool IsOverride(MethodInfo method)
   {
      return ! method.Equals(method.GetBaseDefinition());
   }
}

public class Example
{
   public static void Main()
   {
      MethodInfo equals = typeof(Int32).GetMethod("Equals", 
                                        new Type[] { typeof(Object) } );
      Console.WriteLine("{0}.{1} is inherited: {2}", 
                        equals.ReflectedType.Name, equals.Name,
                        ReflectionUtilities.IsOverride(equals));
      
      equals = typeof(Object).GetMethod("Equals", 
                                        new Type[] { typeof(Object) } );
      Console.WriteLine("{0}.{1} is inherited: {2}", 
                        equals.ReflectedType.Name, equals.Name,
                        ReflectionUtilities.IsOverride(equals));
   }
}
开发者ID:.NET开发者,项目名称:System.Reflection,代码行数:29,代码来源:MethodInfo.GetBaseDefinition

输出:

Int32.Equals is inherited: True
Object.Equals is inherited: False


注:本文中的System.Reflection.MethodInfo.GetBaseDefinition方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。