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

 

示例下載


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM