【发布时间】:2010-09-22 10:12:19
【问题描述】:
我正在编写一个使用反射的克隆方法。如何使用反射检测属性是索引属性?例如:
public string[] Items
{
get;
set;
}
到目前为止我的方法:
public static T Clone<T>(T from, List<string> propertiesToIgnore) where T : new()
{
T to = new T();
Type myType = from.GetType();
PropertyInfo[] myProperties = myType.GetProperties();
for (int i = 0; i < myProperties.Length; i++)
{
if (myProperties[i].CanWrite && !propertiesToIgnore.Contains(myProperties[i].Name))
{
myProperties[i].SetValue(to,myProperties[i].GetValue(from,null),null);
}
}
return to;
}
【问题讨论】:
-
那不是索引属性,是返回数组的属性。
-
这个问题需要版主修改。这是查找索引器属性的最佳 google 结果,但这不是代码示例说明的内容。下面的答案一半回答了问题,一半回答了代码示例。
标签: c# reflection clone