学生字典类(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();
}
}
}