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

c# 反射简单用法例子

时间:2014-06-03 02:41:50      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:winform   c   class   code   a   tar   

闲来无事,看了下反射的实现,写了个小例子。


新建一个类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace WfmReflection
{
    class MyClass
    {
        public string m;
        public void test() {

        }
        public int MyProperty { get; set; }
    }
}


建一个类库

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Test
{
    public class MyTest
    {
        private string _name;
        private int _age;

        public string name
        {
            get{return _name;}
            set { _name = value; }
        }
        public int age
        {
            get { return _age; }
            set { _age = value; }
        }

        public MyTest(string name, int age)
        {
            _age = age;
            _name = name;
        }

        public string GetTestInfo()
        {
            return _name + ":" + _age.ToString();
        }
    }
}


winform实例:

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.Reflection;

namespace WfmReflection
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        //获取类型信息
        private void button1_Click(object sender, EventArgs e)
        {
            Type type = typeof(MyClass);

            string strInfo = "";
            strInfo = "类型名:" + type.Name;
            strInfo += "\n\t" + "类全名:" + type.FullName;
            strInfo += "\n\t" + "命名空间名:" + type.Namespace;
            strInfo += "\n\t" + "程序集名:" + type.Assembly;
            strInfo += "\n\t" + "模块名:" + type.Module;
            strInfo += "\n\t" + "基类名:" + type.BaseType;
            strInfo += "\n\t" + "是否类:" + type.IsClass;
            strInfo += "\n\t" + "类的公共成员:";
         
            MemberInfo[] memberInfos = type.GetMembers();//得到所有公共成员
            foreach (var item in memberInfos)
            {
                strInfo += "\n\t" + string.Format("{0}:{1}", item.MemberType, item);
           }
            richTextBox1.Text = strInfo;
        }

        //获取程序集信息
        private void button2_Click(object sender, EventArgs e)
        {
            string strInfo = "";
            //获取当前执行代码的程序集
            Assembly assem = Assembly.GetExecutingAssembly();
            strInfo += "\n\t" + "程序集全名:" + assem.FullName;         
            strInfo += "\n\t" +"程序集的版本:" + assem.GetName().Version;
            strInfo += "\n\t" +"程序集初始位置:" + assem.CodeBase;
            strInfo += "\n\t" +"程序集位置:" + assem.Location;
            strInfo += "\n\t" +"程序集入口:" + assem.EntryPoint;     
            Type[] types = assem.GetTypes();
            strInfo += "\n\t" +"程序集下包含的类型:";
            foreach (var item in types)
            {
                strInfo += "\n\t" +"类:" + item.Name;
            }
            richTextBox1.Text = strInfo;
        }

        //反射调用方法
        private void button3_Click(object sender, EventArgs e)
        {
            System.Reflection.Assembly ass = Assembly.LoadFrom(AppDomain.CurrentDomain.BaseDirectory + "bin\\Test.dll"); //加载DLL
            System.Type t = ass.GetType("Test.MyTest");//获得类型(命名空间中的类)

            object o = System.Activator.CreateInstance(t,new object[] { "xiaoming", 19 });
            
            System.Reflection.MethodInfo mi = t.GetMethod("GetTestInfo");//获得方法

            string result=(string) mi.Invoke(o,new object[]{});//调用方法
            richTextBox1.Text = result;
        }
    }
}



c# 反射简单用法例子,布布扣,bubuko.com

c# 反射简单用法例子

标签:winform   c   class   code   a   tar   

原文地址:http://blog.csdn.net/hws1058648831a/article/details/27536677

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