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


C# Type.GetMethod方法代码示例

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


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

示例1: MethodA

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

class Program
{
    // Methods to get:

    public void MethodA(int i, int j) { }

    public void MethodA(int[] i) { }

    public unsafe void MethodA(int* i) { }

    public void MethodA(ref int r) {}

    // Method that takes an out parameter:
    public void MethodA(int i, out int o) { o = 100;}

  static void Main(string[] args)
  {
    MethodInfo mInfo;

    // Get MethodA(int i, int j)
    mInfo = typeof(Program).GetMethod("MethodA",
        BindingFlags.Public | BindingFlags.Instance,
        null,
        CallingConventions.Any,
        new Type[] { typeof(int), typeof(int) },
        null);
    Console.WriteLine("Found method: {0}", mInfo);

    // Get MethodA(int[] i)
    mInfo = typeof(Program).GetMethod("MethodA",
        BindingFlags.Public | BindingFlags.Instance,
        null,
        CallingConventions.Any,
        new Type[] { typeof(int[]) },
        null);
    Console.WriteLine("Found method: {0}", mInfo);

    // Get MethodA(int* i)
    mInfo = typeof(Program).GetMethod("MethodA",
        BindingFlags.Public | BindingFlags.Instance,
        null,
        CallingConventions.Any,
        new Type[] { typeof(int).MakePointerType() },
        null);
    Console.WriteLine("Found method: {0}", mInfo);

    // Get MethodA(ref int r)
    mInfo = typeof(Program).GetMethod("MethodA",
        BindingFlags.Public | BindingFlags.Instance,
        null,
        CallingConventions.Any,
        new Type[] { typeof(int).MakeByRefType() },
        null);
    Console.WriteLine("Found method: {0}", mInfo);

    // Get MethodA(int i, out int o)
    mInfo = typeof(Program).GetMethod("MethodA",
        BindingFlags.Public | BindingFlags.Instance,
        null,
        CallingConventions.Any,
        new Type[] { typeof(int), typeof(int).MakeByRefType() },
        null);
    Console.WriteLine("Found method: {0}", mInfo);
  }
}
开发者ID:.NET开发者,项目名称:System,代码行数:69,代码来源:Type.GetMethod

示例2: MethodA

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

class Program
{
    // Methods to get:

    public void MethodA(int i, int j) { }

    public void MethodA(int[] i) { }

    public unsafe void MethodA(int* i) { }

    public void MethodA(ref int r) {}

    // Method that takes an out parameter.
    public void MethodA(int i, out int o) { o = 100; }

  static void Main(string[] args)
  {
    MethodInfo mInfo;

    // Get MethodA(int i, int j)
    mInfo = typeof(Program).GetMethod("MethodA",
        BindingFlags.Public | BindingFlags.Instance,
        null,
        new Type[] { typeof(int), typeof(int) },
        null);
    Console.WriteLine("Found method: {0}", mInfo);

    // Get MethodA(int[] i)
    mInfo = typeof(Program).GetMethod("MethodA",
        BindingFlags.Public | BindingFlags.Instance,
        null,
        new Type[] { typeof(int[]) },
        null);
    Console.WriteLine("Found method: {0}", mInfo);

    // Get MethodA(int* i)
    mInfo = typeof(Program).GetMethod("MethodA",
        BindingFlags.Public | BindingFlags.Instance,
        null,
        new Type[] { typeof(int).MakePointerType() },
        null);
    Console.WriteLine("Found method: {0}", mInfo);

    // Get MethodA(ref int r)
    mInfo = typeof(Program).GetMethod("MethodA",
        BindingFlags.Public | BindingFlags.Instance,
        null,
        new Type[] { typeof(int).MakeByRefType() },
        null);
    Console.WriteLine("Found method: {0}", mInfo);

    // Get MethodA(int i, out int o)
    mInfo = typeof(Program).GetMethod("MethodA",
        BindingFlags.Public | BindingFlags.Instance,
        null,
        new Type[] { typeof(int), typeof(int).MakeByRefType() },
        null);
    Console.WriteLine("Found method: {0}", mInfo);
  }
}
开发者ID:.NET开发者,项目名称:System,代码行数:64,代码来源:Type.GetMethod

示例3: MethodA

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

class Program
{
    // Methods to get:

    public void MethodA(int i, int j) { }

    public void MethodA(int[] i) { }

    public unsafe void MethodA(int* i) { }

    public void MethodA(ref int r) {}

    // Method that takes an out parameter:
    public void MethodA(int i, out int o) { o = 100;}

  static void Main(string[] args)
  {
    MethodInfo mInfo;

    // Get MethodA(int i, int i)
    mInfo = typeof(Program).GetMethod("MethodA",
        new Type[] { typeof(int), typeof(int) });
    Console.WriteLine("Found method: {0}", mInfo);

    // Get MethodA(int[] i)
    mInfo = typeof(Program).GetMethod("MethodA",
        new Type[] { typeof(int[]) });
    Console.WriteLine("Found method: {0}", mInfo);

    // Get MethodA(int* i)
    mInfo = typeof(Program).GetMethod("MethodA",
        new Type[] { typeof(int).MakePointerType() });
    Console.WriteLine("Found method: {0}", mInfo);

    // Get MethodA(ref int r)
    mInfo = typeof(Program).GetMethod("MethodA",
        new Type[] { typeof(int).MakeByRefType() });
    Console.WriteLine("Found method: {0}", mInfo);

    // Get MethodA(int i, out int o)
    mInfo = typeof(Program).GetMethod("MethodA",
        new Type[] { typeof(int), typeof(int).MakeByRefType() });
    Console.WriteLine("Found method: {0}", mInfo);
  }
}
开发者ID:.NET开发者,项目名称:System,代码行数:49,代码来源:Type.GetMethod

示例4: Main

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

public class Example
{
   public static void Main()
   {
      // Get a Type object that represents a non-generic type.
      GetAddMethod(typeof(ArrayList));

      var list = new List<String>();
      // Get a Type object that represents a constructed generic type.
      Type closed = list.GetType();
      GetAddMethod(closed);
      
      // Get a Type object that represents an open generic type.
      Type open = typeof(List<>);
      GetAddMethod(open);
   }

   private static void GetAddMethod(Type typ)
   {
      MethodInfo method;
      // Determine if this is a generic type.
      if (typ.IsGenericType) {
         // Is it an open generic type?
         if (typ.ContainsGenericParameters)
            method = typ.GetMethod("Add", typ.GetGenericArguments());
         // Get closed generic type arguments.
         else
            method = typ.GetMethod("Add", typ.GenericTypeArguments);
      }
      // This is not a generic type.
      else {
         method = typ.GetMethod("Add", new Type[] { typeof(Object) } );
      }

      // Test if an Add method was found.
      if (method == null) { 
         Console.WriteLine("No Add method found.");
         return;
      }   
      
      Type t = method.ReflectedType;
      Console.Write("{0}.{1}.{2}(", t.Namespace, t.Name, method.Name);
      ParameterInfo[] parms = method.GetParameters();
      for (int ctr = 0; ctr < parms.Length; ctr++)
         Console.Write("{0}{1}", parms[ctr].ParameterType.Name, 
                       ctr < parms.Length - 1 ? ", " : "");

      Console.WriteLine(")");
   }   
}
开发者ID:.NET开发者,项目名称:System,代码行数:56,代码来源:Type.GetMethod

输出:

System.Collections.ArrayList.Add(Object)
System.Collections.Generic.List`1.Add(String)
System.Collections.Generic.List`1.Add(T)

示例5: MethodA

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

class Program
{

    // Method to get:
    public void MethodA() { }

    static void Main(string[] args)
    {

        // Get MethodA()
        MethodInfo mInfo = typeof(Program).GetMethod("MethodA",
            BindingFlags.Public | BindingFlags.Instance);
        Console.WriteLine("Found method: {0}", mInfo);
    }
}
开发者ID:.NET开发者,项目名称:System,代码行数:19,代码来源:Type.GetMethod

示例6: DisplayValue

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

public class TestClass
{
   public void DisplayValue(String s)
   {
      Console.WriteLine(s);
   }
   
   public void DisplayValue(String s, params Object[] values)
   {
      Console.WriteLine(s, values);
   }
   
   public static bool Equals(TestClass t1, TestClass t2)
   {
      return Object.ReferenceEquals(t1, t2);
   }
   
   public bool Equals(TestClass t) 
   {
      return Object.ReferenceEquals(this, t);
   }          
}

public class Example
{
   public static void Main()
   {
      Type t = typeof(TestClass);
      
      RetrieveMethod(t, "DisplayValue", BindingFlags.Public | BindingFlags.Instance);

      RetrieveMethod(t, "Equals", BindingFlags.Public | BindingFlags.Instance);
      
      RetrieveMethod(t, "Equals", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
      
      RetrieveMethod(t, "Equals", BindingFlags.Public | BindingFlags.Static);
   }
   
   private static void RetrieveMethod(Type t, String name, BindingFlags flags)
   {
      try {
         MethodInfo m = t.GetMethod(name, flags);
         if (m != null) {
            Console.Write("{0}.{1}(", t.Name, m.Name);
            ParameterInfo[] parms= m.GetParameters();
            for (int ctr = 0; ctr <parms.Length; ctr++) {
               Console.Write(parms[ctr].ParameterType.Name);
               if (ctr < parms.Length - 1) 
                  Console.Write(", ");
            }
            Console.WriteLine(")");
         }
         else {
            Console.WriteLine("Method not found");
         }
      }
      catch (AmbiguousMatchException) {
         Console.WriteLine("The following duplicate matches were found:");
         MethodInfo[] methods = t.GetMethods(flags);
         foreach (var method in methods) {
            if (method.Name != name) continue;

            Console.Write("   {0}.{1}(", t.Name, method.Name);
            ParameterInfo[] parms = method.GetParameters();
            for (int ctr = 0; ctr < parms.Length; ctr++) {
               Console.Write(parms[ctr].ParameterType.Name);
               if (ctr < parms.Length - 1) 
                  Console.Write(", ");
            }
            Console.WriteLine(")");
         } 
      }         
      Console.WriteLine();
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:79,代码来源:Type.GetMethod

输出:

The following duplicate matches were found:
TestClass.DisplayValue(String)
TestClass.DisplayValue(String, Object[])

The following duplicate matches were found:
TestClass.Equals(TestClass)
TestClass.Equals(Object)

TestClass.Equals(TestClass)

TestClass.Equals(TestClass, TestClass)

示例7: MethodA

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

class Program
{

    // Method to get:
    public void MethodA() { }

    static void Main(string[] args)
    {

        // Get MethodA()
        MethodInfo mInfo = typeof(Program).GetMethod("MethodA");
        Console.WriteLine("Found method: {0}", mInfo);
    }
}
开发者ID:.NET开发者,项目名称:System,代码行数:18,代码来源:Type.GetMethod

示例8: ToString

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

public class Person
{
   public String FirstName;
   public String LastName;
   
   public override String ToString()
   {
      return (FirstName + " " + LastName).Trim();
   }
}

public class Example
{
   public static void Main()
   {
      Type t = typeof(Person);
      RetrieveMethod(t, "ToString");
      
      t = typeof(Int32);
      RetrieveMethod(t, "ToString");
   }

   private static void RetrieveMethod(Type t, String name)
   {   
      try {
         MethodInfo m = t.GetMethod(name);
         if (m != null) 
            Console.WriteLine("{0}.{1}: {2} method", m.ReflectedType.Name,
                              m.Name, m.IsStatic ? "Static" : "Instance");    
         else
            Console.WriteLine("{0}.ToString method not found", t.Name);
      }
      catch (AmbiguousMatchException) {
         Console.WriteLine("{0}.{1} has multiple public overloads.", 
                           t.Name, name);
      }
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:42,代码来源:Type.GetMethod

输出:

Person.ToString: Instance method
Int32.ToString has multiple public overloads.

示例9: Type.GetMethod(String methodName);

//引入命名空间
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;

public class MainClass
{
    public static void Main()
    {
        Type typeInfo = typeof(object);
        MethodInfo methInfo = typeInfo.GetMethod("ToString");
        Console.WriteLine("Info: {0}", methInfo);

    }
}
开发者ID:C#程序员,项目名称:System,代码行数:18,代码来源:Type.GetMethod


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