Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 转换列表<;对象>;到对象类别:列表<;对象>;_C# - Fatal编程技术网

C# 转换列表<;对象>;到对象类别:列表<;对象>;

C# 转换列表<;对象>;到对象类别:列表<;对象>;,c#,C#,是否可以将列表转换为一类文档:列表 例如,我有一个类似这样的类: public class Documents : List<Document> { } 公共类文档:列表 { } 但是我需要将我的列表转换为文档 (由于缺乏这方面的信息,我认为这是不可能的,或者我严重错误地使用了这些对象。通常不能将基类的对象转换为派生类,尤其不能将List转换为MyType:List 在不改变设计的情况下,唯一的选择是构造文档的新实例,并将添加项从原始列表复制到该新实例中(可能存在以列表为参数的

是否可以将
列表
转换为一类
文档:列表

例如,我有一个类似这样的类:

public class Documents : List<Document>
{

}
公共类文档:列表
{
}
但是我需要将我的
列表
转换为
文档


(由于缺乏这方面的信息,我认为这是不可能的,或者我严重错误地使用了这些对象。

通常不能将基类的对象转换为派生类,尤其不能将
List
转换为
MyType:List

在不改变设计的情况下,唯一的选择是构造
文档的新实例
,并将添加项从原始列表复制到该新实例中(可能存在以列表为参数的构造函数)。

方法#1 目前没有默认的内置方式将
List
强制转换为
Objects:List
,但是您可以将列表转换为:

这似乎有点好用,因为有以下好处:

  • 一个很好的
    文档包装器
  • 实际列表上的安全性更高
  • 对列表的更多控制
  • 只实现您真正需要的方法的能力
  • (如果您需要列表的功能,您仍然可以返回您的安全列表!)

你不能。你将需要构造一个新的
文档
对象。简单的强制转换将不起作用。不建议对
列表
进行子类化。也许你应该实现
IEnumerable
并将列表传递给
文档
?但是,如果
文档
没有任何实现,最好是暗示使用
List
而不是
Documents
@robbypass-in,你的意思是……?@beardedmogul在构造函数中提供
Documents
的文档列表,或者作为属性。你为什么想要另一个类?你想添加任何额外的功能或属性吗?读得好,但我们的情况不同。Documents,在这种情况下,它只是一个文档列表。我只是在寻找一种更干净的方法来创建复数文档。@BeardeMogul添加了一种更干净的方法!我偶然地重新审视了这个答案,我可以说,方法2是天才:)我不完全理解结果会是什么,尽管我仍然不完全理解它在做什么,我做了足够多的工作来实现它。然而,我有一个问题。我无法在上面实现“Where”。“你能帮我一把吗?”胡子大亨听了很高兴!:)稍后,我将制定一个解决方案,但同时您可以简单地使用ToList()上的Where方法。简单但有效;)我确实得到了一个有效的解决方案。我的问题是,我让它在一个已经初始化的对象上执行,所以如果我运行一次“where”,它就会像我预期的那样工作,但是再次运行它只会对前面的“where's”结果进行评估。我将其更改为复制当前对象,并在复制的数据上返回“where”,保持原始对象不变。
public static Documents CastToDocuments(this List<Document> docs)
{
    var toRet = new Documents();
    toRet.AddRange(docs);
    return toRet;
}
/// <summary>
/// Represents a list of documents.
/// </summary>
public class Documents
{
    /// <summary>
    /// Initialises the private list of documents.
    /// </summary>
    public Documents()
    {
        _docs = new List<Document>();
    }

    /// <summary>
    /// Add a speified document.
    /// </summary>
    /// <param name="doc">This document will be added to the saved documents.</param>
    public void Add(Document doc)
    {
        _docs.Add(doc);
    }

    /// <summary>
    /// Remove a specific document.
    /// </summary>
    /// <param name="doc">This document will be removed from the saved documents.</param>
    public void Remove(Document doc)
    {
        _docs.Remove(doc);
    }

    /// <summary>
    /// Removes all saved documents.
    /// </summary>
    public void Clear()
    {
        _docs.Clear();
    }

    /// <summary>
    /// "Casts" this instance to a list of documents.
    /// </summary>
    /// <returns>Returns all documents inside a list.</returns>
    public List<Document> ToList() => _docs;

    /// <summary>
    /// A list of documents.
    /// </summary>
    private readonly List<Document> _docs;
}