我试图弄清楚如何通过 API 访问对象中的值,但运气不佳。有一些文档,但不多。我可以访问一些信息,但我要查找的信息存在于该软件正在使用的数据库的关键字字段中。我可以打印出对象类型,但不能打印出实际对象中的值。
这是我的代码:
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;
}
}
这仅输出对象类型。我尝试使用 foreach 循环输出 ItemInfo.Item 对象中的所有内容,但收到此错误: Foreach 语句无法对“object”类型的变量进行操作,因为“object”不包含“GetEnumerator”的公共定义
本质上,我正在尝试访问关键字字段中的值以供稍后使用。
错误信息非常明确 - 你只能使用foreach
在具有公共定义的对象上循环GetEnumerator
method.
来自foreach,在(C# 参考) https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/foreach-in微软文档中的页面:
The foreach
语句为数组或对象集合中的每个元素重复一组嵌入语句,实现System.Collections.IEnumerable https://learn.microsoft.com/en-us/dotnet/api/system.collections.ienumerable?view=netframework-4.7.1 or System.Collections.Generic.IEnumerable<T> https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.ienumerable-1?view=netframework-4.7.1界面。
这意味着您不能使用foreach
在您想要的任何类(或结构)的任何实例上循环,因为无论用于什么in
的一部分foreach
循环必须提供一个枚举器。
有趣的事实:作为神秘性 https://stackoverflow.com/users/259769/enigmativity指出在这个答案 https://stackoverflow.com/a/47786380/3094533,要使 foreach 循环工作,您真正需要的只是一个名为的公共方法GetEnumerator()
,并让该公共方法返回一个带有几个方法和一个属性的实例,即void Reset()
, bool MoveNext()
, and T Current { get; private set; }
, Where T
是某种类型。
这意味着您不需要将您的类声明为实现IEnumerable
or the IEnumerable<T>
接口,并且您的枚举器不需要声明为实现IEnumerator
or IEnumerator<T>
接口 - 您提供定义相关接口的方法和属性就足够了 - 这就是 c# 编译器要求使用的所有内容foreach
loop.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)