[英]Access the field value from IList in c#
I have a IList<T>
which has data and i want to access the field value from the IList. 我有一个具有数据的IList<T>
,我想从IList访问字段值。
I have written below code to get field value 我写了下面的代码来获取字段值
IList<T> source;
source.FirstOrDefault().Field("ParentID")
I want to access ParentID field value. 我想访问ParentID字段值。
But it is giving following error while building the solution: 但是在构建解决方案时会出现以下错误:
"'T' does not contain a definition for 'Field' and no extension method 'Field' accepting a first argument of type 'T' could be found (are you missing a using directive or an assembly reference?)" “'T'不包含'Field'的定义,找不到扩展方法'Field'接受类型为'T'的第一个参数(您是否缺少using指令或程序集引用?)”
I have added System.Xml, System.Xml.Linq and System.Collections.Generic assembly, but still it doesn't work. 我添加了System.Xml,System.Xml.Linq和System.Collections.Generic程序集,但仍然无法正常工作。 Can anybody help? 有人可以帮忙吗?
Here is some generics 101. 这是一些泛型101。
Let's say you had a class named Node
, which contained a property named ParentID
. 假设您有一个名为Node
的类,其中包含一个名为ParentID
的属性。
public class Node
{
public int ParentId { get; set; }
}
Now you define a list of nodes. 现在,您定义节点列表。 Basically an array of objects. 基本上是一个对象数组。 In List<T>
the T
refers to the type of objects in your list. 在List<T>
, T
列表中对象的类型。 So you would define it as such: 因此,您可以这样定义它:
IList<Node> source;
Then you would need to populate your list named source
. 然后,您需要填充名为source
的列表。
And then you could select the ParentId
with the code you wrote. 然后,您可以选择带有您编写的代码的ParentId
。
source.FirstOrDefault().Field("ParentID")
将source.FirstOrDefault()强制转换为适当的类,例如: ((Employee)source.FirstOrDefault()).Field("ParentID")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.