簡體   English   中英

訪問類對象列表

[英]Accessing a List of Class objects

我正在嘗試使以下內容起作用:
一個名為Caption的類,我在其中填充一個 List - 將Items添加到 Class 列表中,然后我想在查找方法中引用類值列表

public class Caption
{
    readonly CaptionKey _CaptionKey;    //Enum list
    readonly string _Description;

    public Caption(CaptionKey captionKey, string description)
    {
        _CaptionKey = captionKey;
        _Description = description;
    }

    public CaptionKey CaptionKey { get { return _CaptionKey; } }

    public string Description { get { return _Description; } }

}

這是創建類列表的類

public class InitCaptions

    public static List<Caption> _Captions = new List<Caption>();

// the class access I need

public static string LookupCaption( CaptionKey )
{
   //?  How to return the description for 

}

問題在於引用來自另一個類和進程的類列表。

我可以看到調試器中的值在System.Collections.Generic.List<MyNamespace.Controllers.Captions>我只是不確定如何正確引用它。

我應該添加 - 這是一個 MVC 解決方案 - 所以列表是在應用程序啟動時創建的,但引用調用是從報告中完成的 - 使用 ReportViewer。 該列表顯示在使用 Intellisense 的代碼中,但在運行時 - 該列表不存在。

假設 Simple Dictionary<>不適合您的需求(由於您未指定的原因),那么要從 List 中查找並返回一個項目,您可以使用 List.Find 方法,該方法接受一個委托並像這樣使用:

Caption foundItem = _Captions.Find(delegate (Caption obj) { return obj.Description.equals("Find this Description"); });

如果有多個,那么它將返回它找到的第一個。

還有FindAll (....) 版本,它將返回所有匹配項的新列表。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM