c# - C# 中的列表和列表的值

标签 c# list data-structures for-loop sharepoint-deployment

我来自 PHP,我认为我不理解如何正确地遍历数据结构。 SPFieldCollection.Add 的文档很清楚它需要什么样的参数,所以我写了一个类:

namespace SPAPI.Lists.Add
{
    class AddParams
    {
        public SPFieldType type { get; set; }
        public bool required { get; set; }
    }
}

从那里我创建了一个列表,它可以:

public Create(SPFeatureReceiverProperties properties, Dictionary<string, List<AddParams>> columns, string name, string description, SPListTemplateType type)
{
    SPSite siteCollection = properties.Feature.Parent as SPSite;
    if (siteCollection != null)
    {
        SPWeb web = siteCollection.RootWeb;
        web.Lists.Add(name, description, type);
        web.Update();

        // Add the new list and the new content.
        SPList spList = web.Lists[name];
        foreach(KeyValuePair<string, List<AddParams>> col in columns){
            spList.Fields.Add(col.Key, col.Value[0], col.Value[1]);
        }

        // More Code ...
    }
}

问题是,它不喜欢 col.Value[0]col.Value[1],因为它们不是 SPFieldType根据严格定义,另一个不是 boolean 。我认为我有正确的想法,但我正在寻找有关如何实现这项工作的指导。

因为 C# 有类型提示,我假设它将使用 AddParams 类来查看类型。

这个想法是传递一系列参数并根据这些参数创建一个新列表。

这更像是一个 C# 问题和数据结构迭代问题,而不是 SP 开发问题。

最佳答案

col.Value[0]col.Value[1] 都是 AddParams 类型。这可能会编译:

spList.Fields.Add(col.Key, col.Value[0].type, col.Value[0].required);

但是您可能需要在 foreach 中使用另一个 foreach:

foreach(AddParams item in col.Value)
{
}

关于c# - C# 中的列表和列表的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16925639/

相关文章:

c# - 根据存储的时区修改 ASP.NET MVC C# 中的日期

javascript - 动态列表复选框问题

java - 对等价类的元素进行分组的数据结构

c# - System.ComponentModel.Win32Exception : Access is denied. ....错误

c# - 是否可以通过 Wcftest 客户端测试 WCF throttle 行为?

c - 打印列表 : 1 2 3 4 5 6 7 8 9 = 1 8 3 6 5 4 7 2 9

algorithm - 如何实现三角身份证明算法

java - 在java中找到第k个最小的数字

c# - 创建两个实现相同接口(interface)的单例服务

list - 在 lisp 函数中使用带有 cons 的嵌套汽车