[英]Accessing a List of Class objects
I'm trying to get the following to work:我正在尝试使以下内容起作用:
A class called Caption
, where I populate a List - adding Items
to the Class list I then want to reference the list of class values, in a lookup method一个名为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; } }
}
Here is the class that creates the class list这是创建类列表的类
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
}
The problem is with referencing the list of classes from another class and process.问题在于引用来自另一个类和进程的类列表。
I can see the values in the debugger are there- System.Collections.Generic.List<MyNamespace.Controllers.Captions>
I'm just not sure how to reference it properly.我可以看到调试器中的值在System.Collections.Generic.List<MyNamespace.Controllers.Captions>
我只是不确定如何正确引用它。
I should add - this is a MVC solution - so the List is created in the application startup, but the reference call is done from a report - using ReportViewer.我应该添加 - 这是一个 MVC 解决方案 - 所以列表是在应用程序启动时创建的,但引用调用是从报告中完成的 - 使用 ReportViewer。 the List shows in the code using Intellisense, but when run - the List is not there.该列表显示在使用 Intellisense 的代码中,但在运行时 - 该列表不存在。
Assuming a Simple Dictionary<>
won't suit your needs (for reasons you have not specified), then to find and return an item from a List you can use the List.Find method, which takes a delegate and is used like this:假设 Simple Dictionary<>
不适合您的需求(由于您未指定的原因),那么要从 List 中查找并返回一个项目,您可以使用 List.Find 方法,该方法接受一个委托并像这样使用:
Caption foundItem = _Captions.Find(delegate (Caption obj) { return obj.Description.equals("Find this Description"); });
If there is more than one, then it will return the first it finds.如果有多个,那么它将返回它找到的第一个。
There is also the FindAll
(....) version of this which will return a new list of all matches items.还有FindAll
(....) 版本,它将返回所有匹配项的新列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.