我来自 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/