2

Is this an array of class property or a property class array? And how do I access each row instead of each element in the class in C#? Thanks!

public partial class AdvancedSearchResult {

    private SearchResults[] searchResultsField;

    /// <remarks/>
    **[System.Xml.Serialization.XmlElementAttribute("SearchResults")]
    public SearchResults[] SearchResults { <-------NOT SURE HOW TO DEFINE THIS
        get {
            return this.searchResultsField;
        }
        set {
            this.searchResultsField = value;
        }
    }**


public partial class SearchResults {

    private int dIDField;

    private bool dIDFieldSpecified;

    private int dRevisionIDField;

    ...............etc
4

3 回答 3

2

首先,我会把你的课写得更短一点:

public partial class AdvancedSearchResult 
{
    [System.Xml.Serialization.XmlElementAttribute("SearchResults")]
    public SearchResults[] SearchResults 
    {
        get 
        set 
    }
}

其次,您可以分配值:

AdvancedSearchResult results = new AdvancedSearchResult();
results.SearchResults = new[]
{
    new SearchResults { ... },
    new SearchResults { ... },
    etc.
}

要检索值,您可以使用索引:

SearchResults firstResult = results.SearchResults[0];

或使用枚举器:

foreach(SearchResults sr in results.SearchResults)
{
    // Something with sr.
}
于 2013-05-13T20:59:06.650 回答
0

SearchResults 是 AdvancedSearchResult 类的属性。它获取/设置名为 searchResultsField 的 SearchResults 项目的私有数组。

按如下方式访问它:

var result=new AdvancedSearchResult();

foreach(var item in result.SearchResults)
{  var myId= item.dIDField; 
   var isSpecified=item.dIDFieldSpecified;
   ...
}
于 2013-05-13T21:08:12.113 回答
0

如果我理解正确:

  1. 不要打电话给你的财产SearchResults,因为那是班级的名字。调用它searchResults
  2. 像这样访问“行”:searchResults[rowNumber].

searchResultsField是一个数组,其中每个成员都是一个SearchResults类实例。(事实上​​)您的财产也是如此。所以它是一个类实例数组

要访问您将使用的属性中的字段searchResults[rowNumber].fieldName(假设它们是公共字段)。

由于您的评论- 这是一个简单的反射示例:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        MyClass myClass = new MyClass() { i = "a", j = "b" };
        FieldInfo[] infos = typeof(MyClass).GetFields();
        foreach (FieldInfo info in infos)
            Text += info.GetValue(myClass);
    }
}


class MyClass
{
    public string i;
    public string j;
}

并在顶部添加:

using System.Reflection;
于 2013-05-13T21:03:12.330 回答