第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

通過字符串標(biāo)識符獲取 list<T> 的值

通過字符串標(biāo)識符獲取 list<T> 的值

C#
猛跑小豬 2022-11-13 14:44:42
我想通過僅用字符串標(biāo)識對象來獲取 List 中項目的值string A ="some_string";              listobject.Add(fruit[0].A);public class apple    {        public string labbnummer { get; set; }        public string red { get; set; }        public string gren { get; set; }        public string blue { get; set; }        public string purple { get; set; }}public List<apple> fruit = new List<apple>();public List<apple> rutten_fruit = new List<apple>();List<string> myfruitlist = new List<string>();myfruitlist.Add("green");  myfruitlist.Add("red"); public void populate{ while (reader.Read())                {                    apple tasty = new apple();                    tasty.green = (string)reader["green"];                    tasty.red = (string)reader["red"];            if (list_nr == 0) { fruit.Add(tasty); }}public void orange(){  foreach (var items in myfruitlist)            {               var A =items;                             rutten_fruit.Add(fruit[0].A.ToString());                    }}它不接受 listobject.Add(fruit[0].A); A 作為標(biāo)識符
查看完整描述

1 回答

?
LEATH

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));


查看完整回答
反對 回復(fù) 2022-11-13
  • 1 回答
  • 0 關(guān)注
  • 139 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號