简体   繁体   English

Foreach语句无法对类型为“对象”的变量进行操作,因为“对象”不包含“ GetEnumerator”的公共定义

[英]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 the System.Collections.IEnumerable or System.Collections.Generic.IEnumerable<T> interface. foreach语句对实现System.Collections.IEnumerableSystem.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. 这意味着您无需将类声明为实现IEnumerableIEnumerable<T>接口,并且不需要将Enumerator声明为实现IEnumeratorIEnumerator<T>接口-只要提供方法就足够了和属性定义了相关的接口-这就是c#编译器要求使用foreach循环的全部。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 foreach语句获取错误无法对类型为的变量进行操作,因为“不包含&#39;GetEnumerator&#39;的公共定义 - Getting error foreach statement cannot operate on variables of type '' because '' does not contain a public definition for 'GetEnumerator' foreach语句无法对类型为“ gerant”的变量进行操作,因为“ gerant”不包含“ GetEnumerator”的公共定义 - foreach statement cannot operate on variables of type 'gerant' because 'gerant' does not contain a public definition for 'GetEnumerator' Foreach语句无法对类型为&#39;?&#39;的变量进行操作 因为“?” 不包含“ GetEnumerator”的公共定义 - Foreach statement cannot operate on variables of type '?' because '?' does not contain a public definition for 'GetEnumerator' 错误:foreach 语句无法对“”类型的变量进行操作,因为“”不包含“GetEnumerator”的公共定义 - Error: foreach statement cannot operate on variables of type '' because '' does not contain a public definition for 'GetEnumerator' foreach语句无法对&#39;Oblig1.Models.User&#39;类型的变量进行操作,因为它不包含&#39;GetEnumerator&#39;的公共定义 - foreach statement cannot operate on variables of type 'Oblig1.Models.User' because it does not contain a public definition for 'GetEnumerator' foreach语句无法对类型的变量进行操作,因为其中不包含“ GetEnumerator”的公共定义 - foreach statement cannot operate on variables of type because does not contain a public definition for 'GetEnumerator' foreach语句不能对&#39;T&#39;类型的变量进行操作,因为&#39;T&#39;不包含&#39;GetEnumerator&#39;的公共定义? - foreach statement cannot operate on variables of type 'T' because 'T' does not contain a public definition for 'GetEnumerator'? foreach语句不能对'int'类型的变量进行操作,因为'int'不包含'GetEnumerator'的公共定义 - foreach statement cannot operate on variables of type 'int' because 'int' does not contain a public definition for 'GetEnumerator' 为什么我会收到此错误消息“foreach 语句无法对类型为‘’的变量进行操作,因为‘’不包含‘GetEnumerator’的公共定义” - Why am I Getting this error" foreach statement cannot operate on variables of type '' because '' not contain a public definition for 'GetEnumerator' " Foreach 语句无法对“Table1”类型的变量进行操作,因为“Table1”不包含“GetEnumerator”的公共实例定义 - Foreach statement cannot operate on variables of type 'Table1' because 'Table1' does not contain a public instance definition for 'GetEnumerator'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM