小编Gre*_*zer的帖子

C#反射索引属性

我正在使用反射编写克隆方法.如何使用反射检测属性是索引属性?例如:

public string[] Items
{
   get;
   set;
}
Run Code Online (Sandbox Code Playgroud)

我的方法到目前为止:

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;
}
Run Code Online (Sandbox Code Playgroud)

c# reflection clone

23
推荐指数
3
解决办法
2万
查看次数

标签 统计

c# ×1

clone ×1

reflection ×1