反射

using System;
using System.Reflection;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    public class Class1
    {
        /// <summary>

        /// 应用程序的主入口点。

        /// </summary>

        [STAThread]

        static void Main(string[] args)
        {

            //

            // TODO: 在此处添加代码以启动应用程序

            //

 

            Love love = new Love();

            Type type = love.GetType();

 

            Object obj = type.InvokeMember(null,BindingFlags.DeclaredOnly |BindingFlags.Public | BindingFlags.NonPublic |BindingFlags.Instance

                    | BindingFlags.CreateInstance, null, null, args);

 

            //调用没有返回值的方法,Display

            type.InvokeMember("Display", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, obj, new object[] { "aldfjdlf" });

 

            //调用有返回值的方法

            int i = (int)type.InvokeMember("GetInt", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, obj, new object[] { 1 });

            Console.WriteLine(i);

 

            //设置属性值

            type.InvokeMember("Name", BindingFlags.SetProperty, null, obj, new string[] { "abc" });

            //获取属性值

            string str = (string)type.InvokeMember("Name", BindingFlags.GetProperty, null, obj, null);

            Console.WriteLine(str);

 

            //设置字段值

            type.InvokeMember("field1", BindingFlags.SetField, null, obj, new object[] { 444 });

 

            //获取字段值

            int f = (int)type.InvokeMember("field1", BindingFlags.GetField, null, obj, null);

            Console.WriteLine(f);

            Console.ReadLine();

        }

    }

    class Love
    {

        public int field1;

        private string _name;

        public Love()

        { }

        public string Name
        {

            get{

                return _name;

            }

            set
            {

                _name = value;

            }

        }

 

        public int GetInt(int a)
        {

            return a;

        }

 

        public void Display(string str)
        {

            Console.WriteLine(str);

        }

    }

}

 

利用反射来实现自定义快捷键:

// KeyDown

protected virtual void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            foreach (XmlNode node in nodeList)
            {
                if (bool.Parse(node.Attributes["control"].Value) && int.Parse(node.Attributes["keycode"].Value) == e.KeyCode.GetHashCode())
                {
                    Type type = this.GetType();

                    Object obj = type.InvokeMember(null, BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.CreateInstance, null, null, new object[] { });

                    type.InvokeMember(node.Attributes["event"].Value, BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic, null, obj, new object[] { null, null });
                }
            }
        }


private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                doc.Load(Application.StartupPath + @"\XMLFile1.xml");

                XmlNode nodeForm = doc.SelectSingleNode(".//HotKey/FormName[@name='"+ this.GetType().Name +"']");

                if (nodeForm != null)
                {
                    nodeList = nodeForm.SelectNodes("Button");
                }
            }
            catch
            {
                MessageBox.Show("Load xml faild");
            }
        }

 

 

<?xml version="1.0" encoding="utf-8" ?>
<CustomConfig>
 <!--热键设置-->
 <HotKey>
  <FormName name="Form1">
   <Button name="button1" control="true" alt="false" shift="false" keycode="83" event="button1_Click"></Button>
   <Button name="button2" control="false" alt="false" shift="false" keycode="65" event="button2_Click"></Button>
  </FormName>
 </HotKey>
</CustomConfig>

posted @ 2008-05-18 17:21  谢杰  阅读(377)  评论(0编辑  收藏  举报