简体   繁体   English

C#Generic List转换为实现List <T>的类

[英]C# Generic List conversion to Class implementing List<T>

The following code produces the issue 以下代码产生问题

Cannot implicitly convert type 'System.Collections.Generic.IEnumberable<DataField<string>>' to 'DataFields'. 无法将类型'System.Collections.Generic.IEnumberable<DataField<string>>'隐式转换为'DataFields'。 An explicit conversion exists (are you missing a cast?). 存在显式转换(您是否错过了演员?)。

How do I get around this? 我该如何解决这个问题? What am I doing wrong? 我究竟做错了什么?

public class DataFields : List<DataField>
{
}

public abstract class DataField
{
    public string Name { get; set; }
}

public class DataField<T> : DataField
{
    public T Value { get; set; }
}

public static DataFields ConvertXML(XMLDocument data) {
    DataFields result = (from d in XDocument.Parse(data.OuterXML).Elements()
                      select new DataField<string>
                      {
                          Name = d.Name.ToString(),
                          Value = d.Value
                      }).ToList();
    return result;
}

Edited: Moving the information below to another question. 编辑:将以下信息移至另一个问题。

Using LINQ to create a List<T> where T : someClass<U> 使用LINQ创建List <T>,其中T:someClass <U>

In addition I would like to be able to do something like the following in this statement, in order to set the type of the value for each. 另外,我希望能够在此语句中执行类似下面的操作,以便为每个语句设置值的类型。 How can I accomplish this. 我怎样才能做到这一点。

select new DataField<[Attribute of element called type]>
{
  Name = d.Name.ToString(),
  Value = d.Value
}

Add the following constructor to the DataFields class 将以下构造函数添加到DataFields

public class DataFields : List<DataField> {
    public DataFields() : base() {}
    public DataFields(IEnumerable<DataField> items) : base(items){}
}  

Then 然后

public static DataFields ConvertXML(XMLDocument data) {      
    var result = (BLAH..).ToList();      
    return new DataFields(result);     
}  

OK I figured out one way to handle this thanks to some insight from you guys 好的,由于你们的一些见解,我找到了解决这个问题的方法

// Dont use this class
// public class DataFields : List<DataField>
// {
// }

public abstract class DataField
{
    public string Name { get; set; }
}

public class DataField<T> : DataField
{
    public T Value { get; set; }
}

public static List<DataField> ConvertXML(XMLDocument data) {  //return List<DataField>
     result = (from d in XDocument.Parse(data.OuterXML).Elements()
                      select new DataField<string>
                      {
                          Name = d.Name.ToString(),
                          Value = d.Value
                      }).Cast<DataField>().ToList();  // use cast
    return result;
}

As the error says you are trying to convert DataField<T> where T is string to DataFields which inherits from List of Datafield and they are not same. 正如错误所示,您正在尝试将其中Tstring DataField<T>转换为继承自数据Datafield ListDataFields ,并且它们不相同。

So 所以

DataField<string> not equals DataFields , you could make your DataFields as a list of string s if you wanted this to work DataFields : List<string> DataField<string>不等于DataFields ,如果你想让DataFields ,你可以将你的DataFields作为string的列表DataFields : List<string>

How about this approach: 这种方法怎么样:

 public static DataFields ConvertXML(XmlDocument data)
        {
            DataFields result = (DataFields)(from d in XDocument.Parse(data.OuterXml).Elements()
                                  select new DataField<string>
                                  {
                                      Name = d.Name.ToString(),
                                      Value = d.Value
                                  }).Cast<DataField>();
            return result;
        }

We know that each element is DataField so we could cast it to that type. 我们知道每个元素都是DataField因此我们可以将它转换为该类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM