1 回答

TA貢獻(xiàn)1936條經(jīng)驗 獲得超7個贊
從技術(shù)上講,您可以嘗試使用Reflection ie按名稱查找屬性:
using System.Reflection;
...
private static T PropertyReader<T>(object value, string name) {
if (null == value)
throw new ArgumentNullException(nameof(value));
else if (null == name)
throw new ArgumentNullException(nameof(name));
var prop = value.GetType().GetProperty(name);
if (null == prop || !prop.CanRead)
throw new ArgumentException($"property {name} has not been found.", nameof(name));
return (T)(Convert.ChangeType(prop.GetValue(value, new object[0]), typeof(T)));
}
然后你可以按如下方式使用它:
listobject.Add(PropertyReader<string>(fruit[0], A));
- 1 回答
- 0 關(guān)注
- 139 瀏覽
添加回答
舉報