C# 正在寻找可以帮助我简化反射对象的.NET ObjectReflector
我有一个类,它接收来自其他未知模块的对象,因此我必须对该对象进行反射,以获取其数据,调用其方法,等等 为了简化代码以反映对象,我构建了以下类:C# 正在寻找可以帮助我简化反射对象的.NET ObjectReflector,c#,.net,reflection,C#,.net,Reflection,我有一个类,它接收来自其他未知模块的对象,因此我必须对该对象进行反射,以获取其数据,调用其方法,等等 为了简化代码以反映对象,我构建了以下类: using System; namespace ApplicationCore.Helpers { class ObjectReflector { public object TheObject { get; set; } public Type TheType { get; set; }
using System;
namespace ApplicationCore.Helpers
{
class ObjectReflector
{
public object TheObject { get; set; }
public Type TheType { get; set; }
public ObjectReflector(object theObject)
{
TheObject = theObject;
TheType = theObject.GetType();
}
public string GetObjectShortName()
{
return TheObject.GetType().Name;
}
public string GetObjectLongName()
{
return TheObject.GetType().ToString();
}
public T GetPropertyValue<T>(string propertyName)
{
return (T)TheType.GetProperty(propertyName).GetValue(TheObject, null);
}
public T GetMethodValue<T>(string methodName, object[] parameters)
{
return (T)TheType.GetMethod(methodName).Invoke(TheObject, parameters);
}
}
}
使用系统;
命名空间ApplicationCore.Helpers
{
类对象反射器
{
公共对象对象对象{get;set;}
公共类型类型{get;set;}
公共对象反射器(对象对象)
{
对象=对象;
TheType=theObject.GetType();
}
公共字符串GetObjectShortName()
{
返回object.GetType().Name;
}
公共字符串GetObjectLongName()
{
返回object.GetType().ToString();
}
公共T GetPropertyValue(字符串propertyName)
{
返回(T)type.GetProperty(propertyName).GetValue(对象,null);
}
公共T GetMethodValue(字符串methodName,对象[]参数)
{
返回(T)type.GetMethod(methodName).Invoke(对象,参数);
}
}
}
因此,我有了漂亮、干净的代码,如下所示:
namespace ApplicationCore.Presenters
{
public class SmartFormPresenter
{
public UserControl View { get; set; }
public string ShortName { get; set; }
public string LongName { get; set; }
public string FirstName { get; set; }
public int Age { get; set; }
public int AgePlusTwo { get; set; }
public SmartFormPresenter(object o)
{
SmartFormView smartFormView = new SmartFormView();
View = smartFormView;
smartFormView.DataContext = this;
ObjectReflector or = new ObjectReflector(o);
ShortName = or.GetObjectShortName();
LongName = or.GetObjectLongName();
FirstName = or.GetPropertyValue<string>("FirstName");
Age = or.GetPropertyValue<int>("Age");
AgePlusTwo = or.GetMethodValue<int>("GetAgeInTwoYears", null);
}
}
}
命名空间应用程序核心演示者
{
公共类SmartFormPresenter
{
公共用户控件视图{get;set;}
公共字符串短名称{get;set;}
公共字符串LongName{get;set;}
公共字符串名{get;set;}
公共整数{get;set;}
公共int-AgePlusTwo{get;set;}
公共SmartFormPresenter(对象o)
{
SmartFormView SmartFormView=新建SmartFormView();
视图=smartFormView;
smartFormView.DataContext=此;
ObjectReflector or=新的ObjectReflector(o);
ShortName=或.GetObjectShortName();
LongName=或.GetObjectLongName();
FirstName=或.GetPropertyValue(“FirstName”);
年龄=或.GetPropertyValue(“年龄”);
AgePlusTwo=或.GetMethodValue(“GetAgeInTwoYears”,null);
}
}
}
但是现在我需要制定一些方法,例如,读取的不是int,而是列表
,这样我就必须得到一个列表
,然后思考“对象”等
所以我认为这是以前做过的在.NET中是否有一种叫做ObjectReflector的工具或类可以帮助我简化上面所做的对象反射?无法在我的评论中发布代码。这可以让您开始:
foreach (PropertyInfo pi in oProps)
{
Type colType = pi.PropertyType;
if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition()
==typeof(Nullable<>)))
{
colType = colType.GetGenericArguments()[0];
}
foreach(oProps中的PropertyInfo pi)
{
类型colType=pi.PropertyType;
if((colType.IsGenericType)&&(colType.GetGenericTypeDefinition()
==类型(可为空)))
{
colType=colType.GetGenericArguments()[0];
}
我不会直接使用属性和方法名称。而是使用linq表达式树来避免它们。这使重构更安全。我在XML文件中将方法和属性名称作为字符串,需要调用未知对象。在这种情况下,如何使用ling表达式树?如果我理解正确,您希望提取一个是的,我可以用GetPropertyValue()方法获取简单类型(int,string),但现在我需要获取未知的类型(在其他模块中定义),例如,我的xml文件说我需要对列表属性中的每个元素调用一个方法。这很棘手。您当然可以检查属性类型是否实现ICollection,然后获取第一个元素并对此进行反思。使用继承时会出现问题。我可以想象性能也是一个问题。您可能希望查看我的Googlecode上的“ModelShreder”项目展示了如何通过动态方法有效地实现属性访问