方法返回类型列表<字符串>或泛型方法

本文关键字:泛型方法 字符串 返回类型 列表 方法 | 更新日期: 2023-09-27 18:36:27

我希望该方法具有特定的返回类型,但不知何故我无法使其工作。

我有一个XML结构:

<Notifications>
   <Alerts>
      <Max>3<Max/>
      <Med>2<Med/>
      <Min>1<Min/>
   </Alerts>
</Notifications>

我想找出MaxMedMin的值。但重点是我根本不想要任何 foreach 循环,我只希望该方法具有 List 返回类型,甚至更好地制作泛型返回类型。

关键是,我没有任何自定义类(我不想拥有它)来填充它的属性。

这就是我有一些远的地方,但我在"List()"annonymus 方法上遇到了错误:

Here it returns:
List<string> items = GetAlerts();
//method to read:
public List<string> GetAlerts()
{
    return xmlDoc1.Descendants("Notifications").Elements("Alerts").Select(s => new
    {
        MAX = s.Element("Max").Value,
        MED = s.Element("Med").Value,
        MIN = s.Element("Min").Value
     }).ToList(); //error on this line
}

----------


这种方法看起来

如何是一个泛型返回类型?这是不行的:

Here it returns: 
List<object> items = setBLL2.GetAlert<List<object>>();
public T GetAlert<T>() where T:class
{
    return (T)xmlDoc1.Descendants("Notifications").Elements("Alerts").Select(s => new
    //ERROR up here before (T
    {
         MAX = s.Element("Max").Value,
         MED = s.Element("Med").Value,
         MIN = s.Element("Min").Value
     }).ToList();
}

错误消息是:

无法将类型"System.Collections.Generic.List"转换为"T"

方法返回类型列表<字符串>或泛型方法

不能跨方法边界传输匿名类型(在本例中为从方法返回的List<T>的泛型类型)。

您应该使用 MaxMedMin 作为属性来定义类或结构,并从您的方法初始化其实例列表。

public class Alert
{
    public string Max { get; set; }
    public string Med { get; set; }
    public string Min { get; set; }
}
public IList<Alert> GetAlerts()
{
    return (xmlDoc1.Descendant("Notifications").Elements("Alerts").Select(s => 
        new Alert
        {
            Max = s.Element("Max").Value,
            Med = s.Element("Med").Value,
            Min = s.Element("Min").Value
        }).ToList();
}

编辑:作为替代方法,您可以返回将属性名称映射到其值的字典列表:

public IList<Dictionary<string,string>> GetAlerts()
{
    return (xmlDoc1.Descendant("Notifications").Elements("Alerts").Select(s => 
        new Dictionary<string,string>
        {
            { "Max", s.Element("Max").Value },
            { "Med", s.Element("Med").Value },
            { "Min", s.Element("Min").Value }
        }).ToList();
}

您可以使用以下代码访问您的值:

string firstMin = alerts[0]["Min"];

试试这个:

void Main()
{
   List<string> alerts =
   XDocument.Parse(Data)
            .Descendants("Alerts")
            .Elements()
            .Select (nd => nd.Value)
            .ToList();
   alerts.ForEach(al => Console.WriteLine ( al ) );     // 3 2 1 on seperate lines
}
// Define other methods and classes here
const string Data = @"<?xml version=""1.0""?>
<Notifications>
   <Alerts>
      <Max>3</Max>
      <Med>2</Med>
      <Min>1</Min>
   </Alerts>
</Notifications>";

不幸的是,我不知道如何在 LINQ 查询中的列表中以单独的条目返回 MAX、MED 和 MIN。 但是,您可以做的是让 LINQ 查询将 MIN、MED 和 MAX 放入 List 对象中,然后返回第一行。

public IList<Alert> GetAlerts() 
{ 
    var xmlDoc1 = XDocument.Parse(XML, LoadOptions.None);
    var entries = xmlDoc1.Descendants("Notifications").Elements("Alerts").Select(s => new
        List<string> {
            s.Element("Max").Value,
            s.Element("Med").Value,
            s.Element("Min").Value
        }).ToList();
    // Make sure we don't get an ArgumentOutOfRangeException
    if (entries.Count > 0)
    {
        return entries[0];
    }
    else
    {
        return new List<string>();
    }
}

PS - MIN、MAX 和 MED 的结束标签格式不正确,"/"应该在名称之前,而不是之后。