我來自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