C#反射索引属性 [英] C# Reflection Indexed Properties
本文介绍了C#反射索引属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我写使用反射的克隆方法。如何检测一个属性是使用反射索引属性?例如:
公共字符串[]项目
{
搞定;
组;
}
我的方法迄今:
公共静态牛逼克隆< T>(T从,列表<串> propertiesToIgnore)其中T:新的()
{
T可=新型T ();
型=的myType from.GetType();
的PropertyInfo [] = myProperties myType.GetProperties();
的for(int i = 0; I< myProperties.Length;我++)
{
如果(myProperties [I] .CanWrite&放大器;&安培;!propertiesToIgnore.Contains( myProperties [I] .Name点))
{
myProperties [I] .SetValue(于myProperties [I] .GetValue(自,空),NULL);
}
}
返回;
}
解决方案
如果(propertyInfo.GetIndexParameters()长度方式> 0)
{
//属性是一个索引
}
I am writing a Clone method using reflection. How do I detect that a property is an indexed property using reflection? For example:
public string[] Items
{
get;
set;
}
My method so far:
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;
}
解决方案
if (propertyInfo.GetIndexParameters().Length > 0)
{
// Property is an indexer
}
这篇关于C#反射索引属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文