岁月总是愈来愈短 日子也总是愈来愈快 珍惜
c# 关于反射 使用详细 示例
////// 批量删除指定类的对象 /// 2011-11-17 Aiice /// /// 需要删除数据对应的类名称 例如:ShippAddress /// 需要删除对象的ID对象组 ///删除成功:ture 删除失败:false private bool DeleteObject(string ClassName,object[] obj) { bool result = false; string str = String.Empty; try { //需要反射类 对应的dll文件或exe文件 string assFile = "D:/WebERP/BusinessData/bin/Debug/Smart.Component.BusinessData.dll"; Assembly assembly =Assembly.GetExecutingAssembly(); if(assFile!="") { assembly = Assembly.LoadFrom(assFile); } //把String的ClassName转换为对应Type Type t = assembly.GetType(ClassName); Type[] tp =new Type[1]; tp[0] = typeof(int); object o; /* Object.Methods 获取类里面方法 * 1,获取类里面全部方法 * System.Reflection.MethodInfo[] methods = t.t.GetMethods(); * * 2,获取类里面指定名称的方法 * GetMethod(方法名称, 方法参数类型) 用于方法重写 * 例如方法: Seek(int ID); Seek(String Key) 我现在要获取Seek(int ID)方法 需要这么设置type(tp)参数 * 方法参数类型初始化 * Type[] tp =new Type[1];tp[0] = typeof(int); * System.Reflection.MethodInfo method = t.GetMethod(methodName,tp); */ System.Reflection.MethodInfo seekMethod = t.GetMethod("Seek",tp); System.Reflection.MethodInfo deleteMethod = t.GetMethod("Delete"); /* Object obj = new Object(SysManager sm) * 所以 System.Activator.CreateInstance 创建一个对象时候需要传人 SysManager 对象 否则无发创建一个新的Object对象 */ o = System.Activator.CreateInstance(t,new object[]{CurrentPortal.SysManager}); //Object.Seek(int ID) obj需要object ID 信息 str=Convert.ToString(seekMethod.Invoke(o,obj)); //如果对象存在 则开始删除对象 if(str=="True") { //Object.Delete() 不需要参数传人参数便可 deleteMethod.Invoke(o,new object[]{}); result = true; } } catch { //Nothing To Do Now } return result; } //方法调用 private void Page_Load(object sender, System.EventArgs e) { MenuId=ConvertToInt(Request.Params["MenuId"]); object[] obj=new object[]{99}; bool objResult = DeleteObject("Smart.Component.BusinessData.ShipAddress",obj); }
| 打印文章 | 这篇文章由Aiice于2011年11月17日 8:42 上午发表在ASP.NET。你可以订阅RSS 2.0 也可以发表评论或引用到你的网站。 |
大约3月前
///删除成功:ture 删除失败:false
/// 批量删除指定类的对象
/// 2011-11-17 Aiice
///
/// 需要删除数据对应的类名称 例如:ShippAddress /// 类对应dll文件位置名称 /// 菜单ID /// 需要删除对象的ID对象组 ///
private bool DeleteObject(string ClassName,string ClassFileName,int menuID,object[] obj)
{
Smart.FrameWork.Menu menu = new Menu(null,CurrentPortal.SysManager,false);
menu.Seek(menuID);
bool result = false;
string str = String.Empty;
try
{
//需要反射类 对应的dll文件或exe文件
string assFile = System.AppDomain.CurrentDomain.BaseDirectory + “bin/”+ClassFileName;
Assembly assembly =Assembly.GetExecutingAssembly();
if(assFile!=”")
{
assembly = Assembly.LoadFrom(assFile);
}
//把String的ClassName转换为对应Type
Type t = assembly.GetType(ClassName);
Type[] tp =new Type[1];
tp[0] = typeof(int);
object o;
/* Object.Methods 获取类里面方法
* 1,获取类里面全部方法
* System.Reflection.MethodInfo[] methods = t.t.GetMethods();
*
* 2,获取类里面指定名称的方法
* GetMethod(方法名称, 方法参数类型) 用于方法重写
* 例如方法: Seek(int ID); Seek(String Key) 我现在要获取Seek(int ID)方法 需要这么设置type(tp)参数
* 方法参数类型初始化
* Type[] tp =new Type[1];tp[0] = typeof(int);
* System.Reflection.MethodInfo method = t.GetMethod(methodName,tp);
*/
/****************************************************************/
System.Reflection.PropertyInfo objectProperty = t.GetProperty(“DocumentHeader”);
if(objectProperty!=null)
{
this.Response.Write(“有headed”);
}
else
{
this.Response.Write(“没有headed”);
}
System.Reflection.MethodInfo[] methodes = t.GetMethods();
foreach(System.Reflection.MethodInfo att in methodes)
{
this.Response.Write(att.Name);
}
/*****************************************************************/
System.Reflection.MethodInfo seekMethod = t.GetMethod(“Seek”,tp);
System.Reflection.MethodInfo deleteMethod = t.GetMethod(“Delete”);
/* Object obj = new Object(SysManager sm)
* 所以 System.Activator.CreateInstance 创建一个对象时候需要传人 SysManager 对象 否则无发创建一个新的Object对象
*/
o = System.Activator.CreateInstance(t,new object[]{CurrentPortal.SysManager});
//Object.Seek(int ID) obj需要object ID 信息
str=Convert.ToString(seekMethod.Invoke(o,obj));
//如果对象存在 则开始删除对象
if(str==”True”)
{
Object currentDocumentObject = t.GetProperty(“OrderID”).GetValue(o,null);
Document newDocumentHeader = new Document(CurrentPortal.SysManager);
newDocumentHeader.Seek(int.Parse(currentDocumentObject.ToString()));
if(newDocumentHeader!=null)
{
bool isCanDelete = newDocumentHeader.UserCanDelete(menu);
if(isCanDelete)
{
//Object.Delete() 不需要参数传人参数便可
//deleteMethod.Invoke(o,new object[]{});
result = true;
}
}
}
}
catch
{
//Nothing To Do Now
}
return result;
}