學生字典類(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< string, string> AttributeDict = new Dictionary< string, string>();
/// <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.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< string, string> AttributeDict = new Dictionary< string, string>();
/// <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];
}
}
}
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< string, string> AttributeDict = new Dictionary< string, string>();
/// <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];
}
}
}
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< string, string> AttributeDict = new Dictionary< string, string>();
/// <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< string, string> dict = field.GetValue(obj) as Dictionary< string, string>;
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();
}
}
}
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< string, string> dict = field.GetValue(obj) as Dictionary< string, string>;
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();
}
}
}