【问题标题】:How to declare and handle dynamicaly lists of lists in C# 1.1如何在 C# 1.1 中动态声明和处理列表列表
【发布时间】:2012-04-01 19:33:48
【问题描述】:

我有一个名为 CatalogItem 的自定义类,我在方法中接收它作为数组参数。

必须根据这些元素的属性之一组装该方法返回的矩阵。

public CatalogItem[][] ReorderCatalogItems(CatalogItem[] items)

属性

public string Id
public string Version
public bool HasVersion
public bool HasDependencies
public string Name
public string DisplayName
public bool IsExportable
public string CatalogName
public string ZipName

但 Visual Studio 2003 不是 2005、2008 或 2010。我上次在这个框架 (1.1) 中编码已经有一段时间了。

我有这个,因为 List 和泛型不存在

StringBuilder zipNames = new StringBuilder();

foreach(CatalogItem item in items)
{
    if(item.ZipName != string.Empty && zipNames.ToString().IndexOf(item.ZipName) == -1)
    {
        zipNames.Append(item.ZipName);
        zipNames.Append("\r\n");
        //StringBuilder.AppendLine(string) is unexistent in fwk 1.1
    }
}

//SplitString is a custom method that does a string.Split with a string parser (unexistent out-of-the-box feat. in 2003)
string[] sZipNames = SplitString(zipNames.ToString(),"\r\n");

CatalogItem[][] orderedItems = new CatalogItem[sZipNames.Length+1][];
///+1 since string.Empty is one of the valid elements 
/// even when not listed on zZipNames (name default)

foreach(CatalogItem item in items)
{
    int i = 0;
    bool Next = true;
    while(Next)
    {
        if(item.ZipName == string.Empty)
        {
            //I'm out of ideas now
        }
        if(sZipNames[i].IndexOf(item.ZipName) == -1)
        {
            //I'm out of ideas now
        }
        i++;
    }
}

@编辑: 哦,为了进一步理解,这是我真正的最后一个目标:创建一个具有这种格式的 xml。我为此开发了一个 XMLDoc 方法,但我需要创建一个控制中断迭代

<?xml version="1.0" encoding="utf-8"?>
<export>
<items outputfile="c:\data\erase\exportbatch.zip">
        <item>
            <catalog>operations</catalog>                                          
            <id>Od4f0f211-4673-49cd-a197-567768b9c00a</id>    
            <name>NewOp</name>                                 
            <dependencies>false</dependencies>
        </item>
        <item>
            <catalog>operations</catalog>                                          
            <id>Oc19efe25-33ec-45b7-9cc1-a813ffbecc61</id>    
            <name>TestAttributes</name>                                                                     
        </item>
    </items>
    <items outputfile="c:\data\erase\exportbatch2.zip">
        <item>
            <catalog>transactions</catalog>                                          
            <id>T15454fe0-7afb-4146-9ec3-83547f96b25a</id>    
            <name>GetAllEmployee</name>
            <dependencies>true</dependencies>
        </item>
        <item>
            <catalog>transactions</catalog>                                          
            <id>Tc37afa55-39e4-416d-9088-011a117cff72</id>    
            <name>CustOrderHist_Test123</name>                    
            <dependencies>false</dependencies>
        </item>
    </items>
</export>

【问题讨论】:

    标签: c# .net-1.1 visual-studio-2003


    【解决方案1】:

    在 .NET 1.1 中,如果您想要“完全”动态大小,您基本上必须使用 ArrayList,其中每个元素都是一个 ArrayList。这是一种痛苦,并且需要到处施法,但这就是生活。

    在这种情况下,我不确定您是否真的需要它 - 您要尝试做什么并不完全清楚,但如果您实际上知道您的“外部”数组的大小可以为每个项目建立一个ArrayList,然后使用:

    CatalogItem[] subArray = (CatalogItem) list.ToArray(typeof(CatalogItem));
    orderedItems[index] = subArray;
    

    将该列表转换为数组,并将其存储在“外部”数组中。

    希望对您有所帮助 - 抱歉无法提供更完整的代码,但正如我所说,我不清楚您想要实现什么确切

    【讨论】:

    • 这是最有用的。我还进行了编辑(添加了我的最终目的)以获得更准确的帮助,因此,如果您认为这更好,请随时提出另一个解决方案。
    • @apacay:好的,我不清楚你为什么不随手添加到 XmlDocument 中。为什么需要中间结果?
    • 因为.. 我不会搜索我要添加的每个项目的属性吗?如果你这样做是否相同(因为无论如何你都必须这样做)...我仍然需要检查项目节点是否已经存在,如果它已经存在就不要写它,而且我不记得如何在 XmlDoc 上执行此操作,我将不得不搜索更多...做你认为值得搜索吗?
    • 你是对的,我找到了这个stackoverflow.com/q/2797238/684646,所以这是个好消息。
    • @apacay:我不得不说我没有遵循所有这些(我仍然没有真正了解你想要实现的目标) - 但听起来你已经得到了答案:)
    猜你喜欢
    • 1970-01-01
    • 2013-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-14
    • 2013-08-08
    • 1970-01-01
    • 2017-05-16
    相关资源
    最近更新 更多