Csharp/C Sharp by API/System.Reflection/MethodInfo

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

MethodInfo.GetParameters()

 
using System;
using System.Reflection;

public class Test
{
    public static void Main(string[] args)
    {
    Assembly a = null;
        AssemblyName asmName;
      asmName = new AssemblyName();
      asmName.Name = "Test";
      Version v = new Version("1.0.454.30104");
      asmName.Version = v;
 
        a = Assembly.Load(asmName);
    Type testClassName = a.GetType("Test");
    MethodInfo mi = testClassName.GetMethod("Main");  
    Console.WriteLine("Here are the params for {0}", mi.Name);
 
    // Show number of params.
    ParameterInfo[] myParams = mi.GetParameters();
    Console.WriteLine("Method has {0} params", myParams.Length);
    
    // Show info about param.
    foreach(ParameterInfo pi in myParams)
    {
      Console.WriteLine("Param name: {0}", pi.Name);
      Console.WriteLine("Position in method: {0}", pi.Position);        
      Console.WriteLine("Param type: {0}", pi.ParameterType);
    }
    }
}


MethodInfo.TypeHandle

 

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");
        RuntimeTypeHandle typeHandle = typeInfo.TypeHandle;
        RuntimeMethodHandle methHandle = methInfo.MethodHandle;
        Console.WriteLine("Handle: {0}", methHandle);
    }
}