反射__delegate_01

Posted csskill

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了反射__delegate_01相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

_delegate respondsToSelector:selector 没有调用方法

java反射_01

33_反射(类加载反射)_讲义

为啥 GraphQL 片段在查询中需要 __typename?

20170320_系统管理_用户管理1_反射得到model实例

反射_获取类的信息