2013-06-04 4 views
2

저는 PHP에서 왔으며 데이터 구조를 올바르게 처리하는 방법을 이해하지 못하고 있다고 생각합니다. 그것을C에서 목록의 목록과 값

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 ... 
    } 
} 

문제는 다음과 같습니다 SPFieldCollection.Add에 대한 문서는 소요 인자의 종류를 아주 분명하다, 그래서 수행하는 클래스 쓴 : 내가 않는 목록을 만들어 거기에서

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

을 하나는 SPFieldType이 아니고 다른 하나는 엄격한 정의로 boolean이 아니기 때문에 col.Value[0], col.Value[1]을 좋아하지 않습니다. 나는 올바른 생각을 가지고 있다고 생각하지만이 일을하는 방법에 대한 지침을 찾고 있습니다.

C#은 형식 힌트가 있기 때문에 형식을 보려면 AddParams 클래스를 사용한다고 가정 했으므로

아이디어는 일련의 매개 변수를 전달하고 해당 매개 변수를 기반으로 새 목록을 만드는 것입니다.

이것은 C# 질문 및 데이터 구조 반복 질문과 SP 개발 질문에 더 가깝습니다.

+0

당신이 캐스팅 시도 가지고 col.Value [0] col.Value [1] 각각의 유형? – Rubixus

+0

@Rubixus 무엇이 던지 ... 둘다 AddParams가 될 것입니다. 단지 첫 번째와 두 번째 것입니다. – DonBoitnott

+0

@Don 아마도 귀하의 질문을 이해하지 못했습니다. 나에게 문제는 col.Value [0]이 SPFieldType으로 Fields에 추가되어야하고 col.Value [1]이 부울이어야한다는 것이 었습니다. 그러나 이고르의 대답을 본 후에, 나는 당신이 무엇을했는지 봅니다. – Rubixus

답변

1

변경

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

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

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) 
{ 
} 
-1

AddParams의 목록이 있습니까? 동일한 필드에 대해 1을 초과하여 FieldType을 (를) 기대합니까?

나는 당신이이 방법을 구현해야한다고 생각 :

public Create(SPFeatureReceiverProperties properties, Dictionary<string, 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(string key in columns.Keys){ 
      spList.Fields.Add(key, columns[key].type, columns[key].required); 
     } 

     // More Code ... 
    } 
}