我应该将此代码提取到方法中吗?

时间:2015-04-10 00:41:12

标签: c# reflection

我使用反射来获取一些值,然后将其传递给对象属性。

我应该将其重构为几种方法吗?如果是这样,我应该为每次通话重构一次。类型,属性等。

//base item
var item = aItem;

//The type of the item
var type = item.GetType();

//The property -- In this example a list of strings -- can be list<int> etc.
var property = type.GetProperty("Strings");

//The type of the property
var propertyType = property.PropertyType;

//Value to be converted value is a object
var value = property.GetValue(item);

//New value changing type -- Was showing a list of list -- Was using this when i had a List<object>
//var newValue = Convert.ChangeType(value, propertyType);

//The object property
ListData = value;

1 个答案:

答案 0 :(得分:3)

方法应该只包含执行一个任务的代码。在您给出的代码中。它通过反射从对象返回列表的内容。

在我看来,你应该放弃它。但要创建一个方法来执行此操作:

object GetListData(object item){
    //your code
    return value; 
}
相关问题