List<object> demo(object a) { List<object> res = new List<object>(); object c = a; Type d = a.GetType(); if (d is IList && d.IsGenericType) { IList list = (IList)c; res = (List<object>)list; //這里可能會有問題,請自行修改,或者用 IList.Cast<object>() 進行轉換 } else { throw new Exception("parm is not my type") } return res; }
首先,這種方式是為了某個方法里的分支操作,可以傳入一個值,也可以傳入一個列表。
都說盡量不要干太多的事兒,可是由於我技術問題只能這么搞。
判斷一個 object 是不是一個 List<T>,其實最中間強制轉換哪里,可能會出錯,轉化一下能好一些。
