码迷,mamicode.com
首页 > 其他好文 > 详细

反射__delegate_01

时间:2016-06-02 23:20:50      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:

1、代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Reflection;

namespace WindowsFormsApplication1
{
    [UnmanagedFunctionPointer(CallingConvention.StdCall)]
    public delegate void D_Test01(int _i);
    [UnmanagedFunctionPointer(CallingConvention.StdCall)]
    public delegate void D_Test02(int _i, string _str, int _j);

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Int32 i32 = (Int32)Activator.CreateInstance(typeof(Int32));
            MessageBox.Show(i32.ToString());
        }

        private void button2_Click(object sender, EventArgs e)
        {
            FfnTest01 = new D_Test01(Test01);
            FfnTest02 = new D_Test02(Test02);

                Type typeConvert = typeof(Convert);
                MethodInfo method2Int32 = typeConvert.GetMethod("To" + "Int32", new Type[] { typeof(string) });
                if (method2Int32 == null)
                {
                    MessageBox.Show("method2Int32 == null");
                    return;
                }
                object obj = method2Int32.Invoke(null, new object[] { "200" });

            Type delegateType = typeof(D_Test01);
            MethodInfo method = delegateType.GetMethod("Invoke");
            Console.WriteLine(method.ReturnType.Name + " (ret)");
            foreach (ParameterInfo param in method.GetParameters())
            {
                Console.WriteLine("{0} {1}", param.ParameterType.Name, param.Name);
            }
            method.Invoke(FfnTest01, new object[] { obj });

            // ***

                object obj01 = method2Int32.Invoke(null, new object[] { "1" });
                object obj03 = method2Int32.Invoke(null, new object[] { "5" });

                MethodInfo method2String = typeConvert.GetMethod("To" + "String", new Type[] { typeof(string) });
                if (method2String == null)
                {
                    MessageBox.Show("method2String == null");
                    return;
                }
                object obj02 = method2String.Invoke(null, new object[] { "AAA" });

            Type delegateType02 = typeof(D_Test02);
            MethodInfo method02 = delegateType02.GetMethod("Invoke");
            method02.Invoke(FfnTest02, new object[] { obj01, obj02, obj03 });
        }

        D_Test01 FfnTest01 = null;//new D_Test01(Test01);
        D_Test02 FfnTest02 = null;

        void Test01(int _i)
        {
            MessageBox.Show("Test01 : "+_i.ToString());
        }

        void Test02(int _i, string _str, int _j)
        {
            MessageBox.Show("Test02 : " + _i.ToString() + " , " + _str + " , " + _j.ToString());
        }
    }
}

 

2、

 

反射__delegate_01

标签:

原文地址:http://www.cnblogs.com/csskill/p/5554521.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!