C#反射(取得方法、属性、变量)

程序结构:


学生字典类(S0001):

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

namespace Dictionary.Class.S0001
{
    /// <summary>
    
/// 学生小字典
    
/// </summary>
    public class StudentDict
    {
        /// <summary>
        
/// 教师名
        
/// </summary>
        private string Teacher = "张老师";
        /// <summary>
        
/// 类名称
        
/// </summary>
        public string ClassName = "高三(1)班";
        /// <summary>
        
/// 简单字典类型
        
/// </summary>
        public Dictionary<stringstring> AttributeDict = new Dictionary<stringstring>();

        /// <summary>
        
/// 教师名称
        
/// </summary>
        public string TeacherName 
        {
            get { return Teacher; }
            set { Teacher = value; }
        }
        /// <summary>
        
/// 构造函数
        
/// </summary>
        public StudentDict()
        {
            AttributeDict.Add("01""张三");
            AttributeDict.Add("02""李四");
            AttributeDict.Add("03""王五");
        }
        /// <summary>
        
/// 根据序号查询姓名
        
/// </summary>
        
/// <param name="strCode"></param>
        
/// <returns></returns>
        public string GetStuNameByCode(string strCode)
        {
            return AttributeDict[strCode];
        }
    }
}
学生字典类:(S0002)

 

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

namespace Dictionary.Class.S0002
{
    /// <summary>
    
/// 学生小字典
    
/// </summary>
    public class StudentDict
    {
        /// <summary>
        
/// 教师名
        
/// </summary>
        private string Teacher = "曹老师";
        /// <summary>
        
/// 类名称
        
/// </summary>
        public string ClassName = "高三(2)班";
        /// <summary>
        
/// 简单字典类型
        
/// </summary>
        public Dictionary<stringstring> AttributeDict = new Dictionary<stringstring>();

        /// <summary>
        
/// 教师名称
        
/// </summary>
        public string TeacherName
        {
            get { return Teacher; }
            set { Teacher = value; }
        }
        /// <summary>
        
/// 构造函数
        
/// </summary>
        public StudentDict()
        {
            AttributeDict.Add("01""赵六");
            AttributeDict.Add("02""钱七");
            AttributeDict.Add("03""周八");
        }
        /// <summary>
        
/// 根据序号查询姓名
        
/// </summary>
        
/// <param name="strCode"></param>
        
/// <returns></returns>
        public string GetStuNameByCode(string strCode)
        {
            return AttributeDict[strCode];
        }
    }


Programe主类:

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
            Type type = assembly.GetType("Dictionary.Class.S0002.StudentDict");     //命名空间名 + 类名
            object obj = Activator.CreateInstance(type, true);

            try
            {
                FieldInfo classField = type.GetField("ClassName");
                Console.WriteLine("班级名称:" + classField.GetValue(obj).ToString());
            }
            catch (Exception ex)
            {
                Console.WriteLine("\t班级名称获取失败:" + ex.Message);
            }

            try
            {
                PropertyInfo TeaNameProperty = type.GetProperty("TeacherName");
                Console.WriteLine("\t教师姓名:" + TeaNameProperty.GetValue(obj, null).ToString());
            }
            catch (Exception ex)
            {
                Console.WriteLine("\t教师姓名获取失败:" + ex.Message);
            }

            foreach (FieldInfo field in type.GetFields())
            {
                try
                {
                    if (field.Name == "AttributeDict")
                    {
                        Dictionary<stringstring> dict = field.GetValue(obj) as Dictionary<stringstring>;

                        foreach (string key in dict.Keys)
                        {
                            Console.WriteLine("\t\t学号:{0} -> 姓名:{1}", key, dict[key]);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message.ToString());
                }
            }

            MethodInfo method = type.GetMethod("GetStuNameByCode");
            string strStuName = (string)method.Invoke(obj, new string[] { "02" });
            Console.WriteLine("\t\t学号【{0}】 的学生姓名为:{1}","02",strStuName);

            Console.ReadLine();
        }
    }
}

 

示例下载

posted on 2012-03-21 09:10  一路前行  阅读(18949)  评论(0编辑  收藏  举报