五月综合缴情婷婷六月,色94色欧美sute亚洲线路二,日韩制服国产精品一区,色噜噜一区二区三区,香港三级午夜理伦三级三

您現(xiàn)在的位置: 365建站網(wǎng) > 365文章 > C#中MethodInfo使用方法和實例(轉(zhuǎn)化為Delegate)

C#中MethodInfo使用方法和實例(轉(zhuǎn)化為Delegate)

文章來源:365jz.com     點擊數(shù):1528    更新時間:2018-06-08 22:46   參與評論

將MethodInfo轉(zhuǎn)化為Delegate的方式

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace MethodInfoInvokeDemo
{
public class ReflectTest
{
public void MethodWithNoParaNoReturn()
{
Console.WriteLine("不帶參數(shù)且不返回值的方法");
}
public string MethodWithNoPara()
{
Console.WriteLine("不帶參數(shù)且有返回值的方法");
return "MethodWithNoPara";
}
public string Method1(string str)
{
Console.WriteLine("帶參數(shù)且有返回值的方法");
return str;
}
public string Method2(string str, int index)
{
Console.WriteLine("帶參數(shù)且有返回值的方法");
return str + index.ToString();
}
public string Method3(string str, out string outStr)
{
outStr = "bbbb";
Console.WriteLine("帶參數(shù)且有返回值的方法");
return str;
}
public static string StaticMethod()
{
Console.WriteLine("靜態(tài)方法");
return "cccc";
}
}
class Program
{
static void Main(string[] args)
{
Type type = typeof(ReflectTest);
object reflectTest = Activator.CreateInstance(type);
//不帶參數(shù)且不返回值的方法的調(diào)用
MethodInfo methodInfo = type.GetMethod("MethodWithNoParaNoReturn");
methodInfo.Invoke(reflectTest, null);
Console.WriteLine();
//不帶參數(shù)且有返回值的方法的調(diào)用
methodInfo = type.GetMethod("MethodWithNoPara");
Console.WriteLine(methodInfo.Invoke(reflectTest, null).ToString());
Console.WriteLine();
//帶參數(shù)且有返回值的方法的調(diào)用
methodInfo = type.GetMethod("Method1", new Type[]{typeof(string)});
Console.WriteLine(methodInfo.Invoke(reflectTest, new object[]{"測試"}).ToString());
Console.WriteLine();
//帶多個參數(shù)且有返回值的方法的調(diào)用
methodInfo = type.GetMethod("Method2", new Type[] { typeof(string), typeof(int) });
Console.WriteLine(methodInfo.Invoke(reflectTest, new object[] { "測試", 100 }).ToString());
//Console.WriteLine();
//methodInfo = type.GetMethod("Method3", new Type[] { typeof(string), typeof(string) });
//string outStr = "";
//Console.WriteLine(methodInfo.Invoke(reflectTest, new object[] { "測試", outStr }).ToString());
Console.WriteLine();
//靜態(tài)方法的調(diào)用
methodInfo = type.GetMethod("StaticMethod"); 
Console.WriteLine(methodInfo.Invoke(null, null).ToString());
Console.ReadKey();
}
}
}


有時再用反射的時候,需要將反射出的方法注冊給某個事件,這是就需要將改方法轉(zhuǎn)化為delegate后才能綁定到對應(yīng)的事件上

可以通過Delegate.CreateDelegate的方法來實現(xiàn),如下:

/// <summary>    
        /// 生成反射過來的MethodInfo到指定類型的委托    
        /// </summary>    
        /// <typeparam name="T">EventArgs泛型類型</typeparam>    
        /// <param name="instance">當(dāng)前對象</param>    
        /// <param name="method">需要轉(zhuǎn)化為delegate的方法</param>    
        /// <returns></returns>    
        public static Delegate CreateDelegateFromMethodInfo<T>(Object instance,MethodInfo method) where T:EventArgs//約束泛型T只能是來自EventArgs類型的    
        {    
    
            Delegate del = Delegate.CreateDelegate(typeof(EventHandler<T>), instance, method);    
            EventHandler<T> mymethod = del as EventHandler<T>;    
            return mymethod;    
        }    
    
        /// <summary>    
        /// 生成反射過來的MethodInfo到指定類型的委托    
        /// </summary>    
        /// <typeparam name="T">EventHandle泛型類型</typeparam>    
        /// <param name="instance">當(dāng)前對象</param>    
        /// <param name="method">需要轉(zhuǎn)化為delegate的方法</param>    
        /// <returns></returns>    
        public static Delegate CreateDelegateFromMethodInfoByDelegate<T>(Object instance, MethodInfo method)    
        {    
    
            Delegate del = Delegate.CreateDelegate(typeof(T), instance, method);    
            EventHandler mymethod = del as EventHandler;    
            return mymethod;    
        }

  


private void button3_Click(object sender, EventArgs e)  
        {  
            var Instance = Activator.CreateInstance(typeof(Form1)) as Form1;  
            MethodInfo methodInfo = typeof(Form1).GetMethod("Method1");   
  
             EventHandler dd = Delegate.CreateDelegate(typeof(EventHandler), Instance,methodInfo) as EventHandler;  
            dd(null, null);  
        }  
public void Method1(object sender, EventArgs e)  
        {  
            MessageBox.Show("ee");  
        }


如對本文有疑問,請?zhí)峤坏浇涣髡搲?,廣大熱心網(wǎng)友會為你解答?。?點擊進入論壇

發(fā)表評論 (1528人查看,0條評論)
請自覺遵守互聯(lián)網(wǎng)相關(guān)的政策法規(guī),嚴(yán)禁發(fā)布色情、暴力、反動的言論。
昵稱:
最新評論
------分隔線----------------------------

其它欄目

· 建站教程
· 365學(xué)習(xí)

業(yè)務(wù)咨詢

· 技術(shù)支持
· 服務(wù)時間:9:00-18:00
365建站網(wǎng)二維碼

Powered by 365建站網(wǎng) RSS地圖 HTML地圖

copyright © 2013-2024 版權(quán)所有 鄂ICP備17013400號