[英]Foreach statement cannot operate on variables of type 'object' because 'object' does not contain a public definition for 'GetEnumerator'
I'm trying to figure out how to access values in an object through an API, but am having no luck. 我试图弄清楚如何通过API访问对象中的值,但是没有运气。 There is some documentation, but not a lot.
有一些文档,但不是很多。 I'm able to access some information, but what I'm looking for exists in a keyword field in a database that the software is using.
我可以访问一些信息,但是我要查找的内容存在于该软件正在使用的数据库的关键字字段中。 I am able to print out the object type, but not the values in the actual object.
我可以打印出对象类型,但不能打印出实际对象中的值。
Here's my code: 这是我的代码:
public class Test
{
public static ServiceConnection ConnectToDocuWare(Uri uri, string userName, string passWord)
{
return ServiceConnection.Create(uri, userName, passWord);
}
static void Main()
{
var userName;
var passWord;
var uri;
var conn = ConnectToDocuWare(uri, userName, passWord);
var org = conn.Organizations[0];
var fileCabinets = org.GetFileCabinetsFromFilecabinetsRelation().FileCabinet;
var fileCabinet = fileCabinets.SingleOrDefault(x => x.Name == "someValue");
string documentID = "1";
int DWID = int.Parse(documentID);
string FCID = fileCabinet.Id;
var dialogInfoItems = fileCabinet.GetDialogInfosFromDialogsRelation();
var dialog = dialogInfoItems.Dialog.SingleOrDefault(x => x.DisplayName == "Some Value").GetDialogFromSelfRelation();
var DocResults = RunQuery(dialog, documentID);
foreach (Document doc in DocResults.Items)
{
var item = doc.GetDocumentFromSelfRelation();
foreach (var itemInfo in item.Fields)
{
if (itemInfo.ItemElementName.ToString() == "Keywords")
{
Console.WriteLine(itemInfo.Item);
}
}
}
Console.ReadLine();
}
public static DocumentsQueryResult RunQuery(Dialog dialog, string DWDocID)
{
var q = new DialogExpression()
{
Operation = DialogExpressionOperation.And,
Condition = new List<DialogExpressionCondition>()
{
DialogExpressionCondition.Create("DWDOCID", DWDocID),
},
Count = 100,
SortOrder = new List<SortedField>
{
SortedField.Create("DWSTOREDATETIME", SortDirection.Desc)
}
};
var queryResult = dialog.GetDocumentsResult(q);
return queryResult;
}
}
This just outputs the object type. 这仅输出对象类型。 I've tried to use a foreach loop to output everything in the ItemInfo.Item object, but I get this error: Foreach statement cannot operate on variables of type 'object' because 'object' does not contain a public definition for 'GetEnumerator'
我尝试使用foreach循环输出ItemInfo.Item对象中的所有内容,但出现此错误:Foreach语句无法对类型为“ object”的变量进行操作,因为“ object”不包含“ GetEnumerator”的公共定义
Essentially I'm trying to access the values in the keyword field to use later. 本质上,我试图访问关键字字段中的值以供以后使用。
The error message is very clear - you can only use a foreach
loop on objects that have a public definition of the GetEnumerator
method. 错误消息非常清楚-您只能对具有
GetEnumerator
方法的公共定义的对象使用foreach
循环。
From the foreach, in (C# Reference) page in Microsoft docs: 从foreach,在Microsoft文档的(C#参考)页面中:
The
foreach
statement repeats a group of embedded statements for each element in an array or an object collection that implements theSystem.Collections.IEnumerable
orSystem.Collections.Generic.IEnumerable<T>
interface.foreach
语句对实现System.Collections.IEnumerable
或System.Collections.Generic.IEnumerable<T>
接口的数组或对象集合中的每个元素重复一组嵌入式语句。
This means that you can't use a foreach
loop on any instance of any class (or struct) you want, because whatever is being used for the in
part of the foreach
loop must provide an enumerator. 这意味着您不能在所需的任何类(或结构)的任何实例上使用
foreach
循环,因为foreach
循环中用于in
部分的任何内容都必须提供枚举器。
Interesting fact: As Enigmativity pointed out in this answer , All you really need for a foreach loop to work is having a public method called GetEnumerator()
, and have that public method returns an instance with a couple of methods and a property, namely void Reset()
, bool MoveNext()
, and T Current { get; private set; }
有趣的事实:正如谜题在此答案中指出的那样,要使foreach循环正常工作,您真正需要的是拥有一个名为
GetEnumerator()
的公共方法,并让该公共方法返回一个带有几个方法和一个属性的实例,即void Reset()
, bool MoveNext()
和T Current { get; private set; }
T Current { get; private set; }
T Current { get; private set; }
, Where T
is some type. T Current { get; private set; }
,其中T
是某种类型。
This means that you are not required to declare your class as implementing the IEnumerable
or the IEnumerable<T>
interface, And your Enumerator is not required to be declared as implementing the IEnumerator
or IEnumerator<T>
interface - it's sufficient that you provide the methods and properties defined it the relevant interfaces - That's all the c# compiler demands to use a foreach
loop. 这意味着您无需将类声明为实现
IEnumerable
或IEnumerable<T>
接口,并且不需要将Enumerator声明为实现IEnumerator
或IEnumerator<T>
接口-只要提供方法就足够了和属性定义了相关的接口-这就是c#编译器要求使用foreach
循环的全部。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.