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();
        }
    }
}

 

示例下载

转载于:https://www.cnblogs.com/zhangpengshou/archive/2012/03/21/2409206.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值