C#.Net反射(Reflaction)技术实例详解
C#.Net反射(Reflaction)技术实例详解
本示例详细介绍了.Net反射,多线程,自定义事件(Event),自定义特性(Custom Attribute)的使用.
/// <summary>
/// 订单管理类(订单模块的业务逻辑)
/// </summary>
public class OrderManager
{
private event OnAddOrder _OnOrderAdded;//事件定义
private IList _MyOrders = new ArrayList(); //订单列表
//
//写个例子测试Customer(实体类),Order(实体类),OrderManager(业务逻辑类)3个类的业务关系.
//
//通常把Customer,Order视为实体类,他们不包含业务方法,但必须定义一个业务逻辑类OrderManager
//来管理业务,那么所有有关业务操作都应该封装在业务逻辑类.
//
#region 演示订单管理类的2个业务方法
/// <summary>
/// 获取指定客户编号的订单
/// </summary>
public IList GetOrderByCustomer(string customer)
{
IList result = new ArrayList();
foreach (Order o in _MyOrders)
if (o.CustomerCode.Equals(customer)) result.Add(o);
return result;
}
/// <summary>
/// 获取所有订单的总数
/// </summary>
/// <returns></returns>
public decimal GetTotalAmount()
{
decimal total = 0;
foreach (Order o in _MyOrders)
total = o.Price * o.Quantity;
return total;
}
#endregion
/// <summary>
/// 往订单表加入一张订单
/// </summary>
public void AddOrder(Order myOrder)
{
//设置一个新编号
myOrder.OrderID = myOrder.OrderID "(" (_MyOrders.Count 1).ToString() ")";
_MyOrders.Add(myOrder);
//触发事件!!!
if (_OnOrderAdded != null) _OnOrderAdded(myOrder);
}
/// <summary>
/// 订单业务类的事件
/// </summary>
public event OnAddOrder OnOrderAdded
{
add
{
_OnOrderAdded = new OnAddOrder(value); //定义事件的Add方法
} //语法请参考C#第4版
remove
{
_OnOrderAdded -= new OnAddOrder(value);//定义事件的remove方法
}//语法请参考C#第4版
}
/// <summary>
/// 返回所有订单
/// </summary>
public IList OrderList { get { return _MyOrders; } }
}
// 来源:www.CSFramework.com, C/S结构框架学习网
扫一扫加作者微信
C#.Net反射(Reflaction)技术实例详解
本示例详细介绍了.Net反射,多线程,自定义事件(Event),自定义特性(Custom Attribute)的使用.
//委托定义-用于增加订单时触发的事件
public delegate void OnAddOrder(Order myOrder);
//委托定义-用于运行的多线程中某一线程访问主线程中的方法
public delegate void OnShowData(int index, int value);
// 来源:www.CSFramework.com, C/S结构框架学习网
public delegate void OnAddOrder(Order myOrder);
//委托定义-用于运行的多线程中某一线程访问主线程中的方法
public delegate void OnShowData(int index, int value);
// 来源:www.CSFramework.com, C/S结构框架学习网
/// <summary>
/// 订单管理类(订单模块的业务逻辑)
/// </summary>
public class OrderManager
{
private event OnAddOrder _OnOrderAdded;//事件定义
private IList _MyOrders = new ArrayList(); //订单列表
//
//写个例子测试Customer(实体类),Order(实体类),OrderManager(业务逻辑类)3个类的业务关系.
//
//通常把Customer,Order视为实体类,他们不包含业务方法,但必须定义一个业务逻辑类OrderManager
//来管理业务,那么所有有关业务操作都应该封装在业务逻辑类.
//
#region 演示订单管理类的2个业务方法
/// <summary>
/// 获取指定客户编号的订单
/// </summary>
public IList GetOrderByCustomer(string customer)
{
IList result = new ArrayList();
foreach (Order o in _MyOrders)
if (o.CustomerCode.Equals(customer)) result.Add(o);
return result;
}
/// <summary>
/// 获取所有订单的总数
/// </summary>
/// <returns></returns>
public decimal GetTotalAmount()
{
decimal total = 0;
foreach (Order o in _MyOrders)
total = o.Price * o.Quantity;
return total;
}
#endregion
/// <summary>
/// 往订单表加入一张订单
/// </summary>
public void AddOrder(Order myOrder)
{
//设置一个新编号
myOrder.OrderID = myOrder.OrderID "(" (_MyOrders.Count 1).ToString() ")";
_MyOrders.Add(myOrder);
//触发事件!!!
if (_OnOrderAdded != null) _OnOrderAdded(myOrder);
}
/// <summary>
/// 订单业务类的事件
/// </summary>
public event OnAddOrder OnOrderAdded
{
add
{
_OnOrderAdded = new OnAddOrder(value); //定义事件的Add方法
} //语法请参考C#第4版
remove
{
_OnOrderAdded -= new OnAddOrder(value);//定义事件的remove方法
}//语法请参考C#第4版
}
/// <summary>
/// 返回所有订单
/// </summary>
public IList OrderList { get { return _MyOrders; } }
}
// 来源:www.CSFramework.com, C/S结构框架学习网
//作业3的业务逻辑类
public class JobManager
{
public void LoadTypes(Assembly asm, ListBox.ObjectCollection items)
{
Type[] types = asm.GetTypes();
items.Clear();
foreach (Type t in types) items.Add(t);
}
public void LoadProperties(Type current, ListBox.ObjectCollection items)
{
PropertyInfo[] ps = current.GetProperties(BindingFlags.Public | BindingFlags.Instance);
items.Clear();
foreach (PropertyInfo p in ps) items.Add(p);
}
public void LoadMethods(Type current, ListBox.ObjectCollection items)
{
MethodInfo[] ms = current.GetMethods();
items.Clear();
foreach (MethodInfo m in ms) items.Add(m);
}
public void LoadEvents(Type current, ListBox.ObjectCollection items)
{
EventInfo[] es = current.GetEvents();
items.Clear();
foreach (EventInfo e in es) items.Add(e);
}
public void LoadFields(Type current, ListBox.ObjectCollection items)
{
FieldInfo[] fs = current.GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
items.Clear();
foreach (FieldInfo f in fs) items.Add(f);
}
public void SetPropertyValue(object instance, string propertyName, object value)
{
PropertyInfo p = instance.GetType().GetProperty(propertyName);
if (p == null) return; //没找到属性,不处理
p.SetValue(instance, value, null);
}
public string GetCustomAttributeValue()
{
StringBuilder result = new StringBuilder();
result.AppendLine("特性的值如下:--------------------------");
//取出指定为MyCustomAttribute类型的特性
//注意:这里是取类的特性.参考Customer类名上的特性定义.
object[] list = typeof(Customer).GetCustomAttributes(typeof(MyCustomAttribute),false);
if (list.Length > 0)
{
MyCustomAttribute att = list[0] as MyCustomAttribute;
result.AppendLine(att.Description);
}
//获取Test方法的用户自定义特性
object[] attrs = typeof(Customer).GetMethod("Test").GetCustomAttributes(false);
if (attrs.Length == 2) //我在Test方法上定义了两个Attribute特性
{
DescriptionAttribute att1 = attrs[0] as DescriptionAttribute;//.NetFramework内定义的
MyCustomAttribute att2 = attrs[1] as MyCustomAttribute; //自定义的
result.AppendLine(att1.Description);
result.AppendLine(att2.Description);
}
return result.ToString();
}
}
// 来源:www.CSFramework.com, C/S结构框架学习网
public class JobManager
{
public void LoadTypes(Assembly asm, ListBox.ObjectCollection items)
{
Type[] types = asm.GetTypes();
items.Clear();
foreach (Type t in types) items.Add(t);
}
public void LoadProperties(Type current, ListBox.ObjectCollection items)
{
PropertyInfo[] ps = current.GetProperties(BindingFlags.Public | BindingFlags.Instance);
items.Clear();
foreach (PropertyInfo p in ps) items.Add(p);
}
public void LoadMethods(Type current, ListBox.ObjectCollection items)
{
MethodInfo[] ms = current.GetMethods();
items.Clear();
foreach (MethodInfo m in ms) items.Add(m);
}
public void LoadEvents(Type current, ListBox.ObjectCollection items)
{
EventInfo[] es = current.GetEvents();
items.Clear();
foreach (EventInfo e in es) items.Add(e);
}
public void LoadFields(Type current, ListBox.ObjectCollection items)
{
FieldInfo[] fs = current.GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
items.Clear();
foreach (FieldInfo f in fs) items.Add(f);
}
public void SetPropertyValue(object instance, string propertyName, object value)
{
PropertyInfo p = instance.GetType().GetProperty(propertyName);
if (p == null) return; //没找到属性,不处理
p.SetValue(instance, value, null);
}
public string GetCustomAttributeValue()
{
StringBuilder result = new StringBuilder();
result.AppendLine("特性的值如下:--------------------------");
//取出指定为MyCustomAttribute类型的特性
//注意:这里是取类的特性.参考Customer类名上的特性定义.
object[] list = typeof(Customer).GetCustomAttributes(typeof(MyCustomAttribute),false);
if (list.Length > 0)
{
MyCustomAttribute att = list[0] as MyCustomAttribute;
result.AppendLine(att.Description);
}
//获取Test方法的用户自定义特性
object[] attrs = typeof(Customer).GetMethod("Test").GetCustomAttributes(false);
if (attrs.Length == 2) //我在Test方法上定义了两个Attribute特性
{
DescriptionAttribute att1 = attrs[0] as DescriptionAttribute;//.NetFramework内定义的
MyCustomAttribute att2 = attrs[1] as MyCustomAttribute; //自定义的
result.AppendLine(att1.Description);
result.AppendLine(att2.Description);
}
return result.ToString();
}
}
// 来源:www.CSFramework.com, C/S结构框架学习网
/// <summary>
/// 客户实体类
/// </summary>
[MyCustomAttribute("MyCustomAttribute:这是Customer类的特性,在类名上定义")]
public class Customer
{
//用户属性
//注意命名规则:
// 1.类的字段变量名建议加"_",用于区别其它方法内的变量.
// 2.当变量较多,更方便排序.
private string _CustomerID;
private string _CustomerName;
private string _CustomerAddress;
//无参构造器
public Customer() { }
//构造器重载
public Customer(string id, string name, string address)
{
_CustomerID = id;
_CustomerName = name;
_CustomerAddress = address;
}
/// <summary>
/// 客户编号
/// </summary>
public string CustomerID
{
get { return _CustomerID; }
set { _CustomerID = value; }
}
/// <summary>
/// 客户名称
/// </summary>
public string CustomerName
{
get { return _CustomerName; }
set { _CustomerName = value; }
}
/// <summary>
/// 客户地址
/// </summary>
public string CustomerAddress
{
get { return _CustomerAddress; }
set { _CustomerAddress = value; }
}
//重写ToString()方法,返回客户名称
public override string ToString()
{
return string.Format("ID:{0}, Name:{1}, Address:{2}", _CustomerID, _CustomerName, _CustomerAddress);
}
private string PrivateProperty
{
get { return ""; }
}
[MyCustomAttribute("MyCustomAttribute:这是用户自定义特性,演示Attribute(特性)的应用")]
[DescriptionAttribute("DescriptionAttribute: 这是.Net自带的Description特性")]
public void Test()
{
//注意:这个方法定义了两个特性,在演示程序内会获取这两个特性并显示他们的值.
}
}
// 来源:www.CSFramework.com, C/S结构框架学习网
/// 客户实体类
/// </summary>
[MyCustomAttribute("MyCustomAttribute:这是Customer类的特性,在类名上定义")]
public class Customer
{
//用户属性
//注意命名规则:
// 1.类的字段变量名建议加"_",用于区别其它方法内的变量.
// 2.当变量较多,更方便排序.
private string _CustomerID;
private string _CustomerName;
private string _CustomerAddress;
//无参构造器
public Customer() { }
//构造器重载
public Customer(string id, string name, string address)
{
_CustomerID = id;
_CustomerName = name;
_CustomerAddress = address;
}
/// <summary>
/// 客户编号
/// </summary>
public string CustomerID
{
get { return _CustomerID; }
set { _CustomerID = value; }
}
/// <summary>
/// 客户名称
/// </summary>
public string CustomerName
{
get { return _CustomerName; }
set { _CustomerName = value; }
}
/// <summary>
/// 客户地址
/// </summary>
public string CustomerAddress
{
get { return _CustomerAddress; }
set { _CustomerAddress = value; }
}
//重写ToString()方法,返回客户名称
public override string ToString()
{
return string.Format("ID:{0}, Name:{1}, Address:{2}", _CustomerID, _CustomerName, _CustomerAddress);
}
private string PrivateProperty
{
get { return ""; }
}
[MyCustomAttribute("MyCustomAttribute:这是用户自定义特性,演示Attribute(特性)的应用")]
[DescriptionAttribute("DescriptionAttribute: 这是.Net自带的Description特性")]
public void Test()
{
//注意:这个方法定义了两个特性,在演示程序内会获取这两个特性并显示他们的值.
}
}
// 来源:www.CSFramework.com, C/S结构框架学习网
扫一扫加作者微信
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网