我来自PHP,我不认为我理解如何正确地浏览数据结构。为SPFieldCollection.Add文档是很清楚什么样的需要论据,所以我写了一个类,它的作用:C#中列表的列表和值#
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]各自的类型? – Rubixus
@Rubixus施展什么...那些都是AddParams,只是第一个和第二个。 – DonBoitnott
@Don也许我不明白你的问题。对我来说,似乎你的问题是col.Value [0]应该作为SPFieldType添加到Fields中,而col.Value [1]应该是布尔值。但在看到伊戈尔的回答后,我明白你的意思。 – Rubixus