2015-11-12 37 views
0

S.O的第一個問題,以及目前所有的新手。使用iList構造函數初始化類<T>?

我想從一個對象現在創建一個Json文件的遊戲。這是主要的對象我想要寫一個文件: (班「塊」和「項目」是iLists,因爲你可能會說)

public string title { get; set; } 
    public string type { get; set; } 
    public string map { get; set; } 
    public string mode { get; set; } 
    public string champion { get; set; } 
    public int sortrank { get; set; } 
    public bool isGlobalForChampions { get; set; } 
    public bool isGlobalForMaps { get; set; } 
    public bool priority { get; set; } 
    public IList<Block> blocks { get; set; } 
    public IList<object> associatedChampions { get; set; } 
    public IList<object> associatedMaps { get; set; } 

    public ItemSet(string _title, string _type, string _map, string _mode, string _champion, int _sortrank, bool _isGlobalForChampions, bool _isGlobalForMaps, bool _priority, IList<Block> _blocks, IList<object> _associatedChampions, IList<object> _associatedMaps) 
    { 
     title = _title; 
     type = _type; 
     map = _map; 
     mode = _mode; 
     champion = _champion; 
     sortrank = _sortrank; 
     isGlobalForChampions = _isGlobalForChampions; 
     isGlobalForMaps = _isGlobalForMaps; 
     priority = _priority; 
     blocks = _blocks; 
     associatedChampions = _associatedChampions; 
     associatedMaps = _associatedMaps; 
    } 

public class Block 
{ 
    public IList<Item> items { get; set; } 
    public string type { get; set; } 
} 

public class Item 
{ 
    public int count { get; set; } 
    public string id { get; set; } 
} 

但我得到一個不同的感覺,我用所有錯誤的方式去解決這個問題。我實際上使用Json到C#生成器來發現(在許多失敗的手動嘗試之後)給我一個我可能需要考慮使用的變量的概念。但是我不能爲了我的生活找出用構造函數初始化iList的語法,並且沒有任何不好的搜索引擎使我更接近這個答案。

當前intialization:(丟掉了最後3個參數,很明顯)

ItemSet itemSetTest = new ItemSet("TEST_ITEM_SET", "custom", "any", "", "Kalista", 0, false, true, false,); 

如果我沒有足夠的提供/右信息,請不要猶豫,說白了!

附帶事情: 我認爲我會像添加其他數組一樣添加到列表中? 這是一個完全可怕的方式嗎?

+1

'的IList '是接口,使用'列表'如'的IList 塊=新列表()' –

+0

參見[對象和在C#集合初始化(https://msdn.microsoft.com/en-us/library/bb384062.aspx) –

回答

1

刪除你有的構造函數,你不需要它。

itemSet = new ItemSet { 
    title="...", 
    ..., 
    blocks=new List<Blocks>() { 
    new Block { 
     type="...", 
     items=new List<Item>() { 
     new Item { count=..., id=...}, 
     new Item { count=..., id=...} 
     } 
    }, 
    new Block { 
     type="...", 
     items=new List<Item>() { 
     new Item { count=..., id=...}, 
     new Item { count=..., id=...} 
     } 
    } 
    } 
}; 
+0

一個新的IList?這甚至有可能嗎?項目=新的IList {... –

+0

愚蠢的我。更新。 –

+0

我認爲一個構造函數是保持初始化在一條線上,我喜歡它的整潔程度。並且謝謝你! –

0

這是要走的路 -

new List<Item>() { 
      new Item(), 
      new Item(), 
      new Item(), 
      new Item() 
    }; 

好運

0

要獲得關於如何實例化類的問題;您需要將新的List項傳遞給您的構造函數。例如:

//add items to these lists and change names if needs be. 
var blocks = new List<Block>(); 
var champs = new List<Object>(); 
var maps = new List<Object>(); 

var itemSetTest = new ItemSet("TEST_ITEM_SET", "custom", "any", "", "Kalista", 0, false, true, false, blocks, champs, maps); 

基本上是一個List實現IList。 IList是一個接口,因此不能實例化。但是因爲List實現了IList,所以你可以將一個List傳遞給你的構造函數,它會接受它。

我希望能回答你的問題。

我也想提一下,你的構造函數是非常大(哦護士)。您可能想要考慮創建更多類並組合這些屬性。您也可以考慮爲這些屬性中的某些屬性設置默認值;例如,如果字符串冠軍通常是「bob」,則可以將其設置爲默認值,並且只有在名稱不同時將冠軍字符串傳遞給構造函數。

我還假設你的ItemSet類(也許想到一個更有意義的名字)實際上是一個類,你只是複製了一些包裝代碼塊?


更新1

當你說的特性結合起來,你的意思是使用 繼承?

不完全是,但也許我沒有正確的字呢。我的意思是將相關屬性分組到他們自己的類中。讓我們一個簡單的例子或人類:

public class Person 
    { 
     public string Name { get; set; } 
     public string Postcode { get; set; } 
     public string Town { get; set; } 
     public string Country { get; set; } 
     public string TwitterUrl { get; set; } 
     public string FacebookUrl { get; set; } 
     public string GooglePlusUrl { get; set; } 

     public Person(string name, string postcode, string town, string country, string twitterurl, string facebookurl, string googleplusurl) 
     { 
      Name = name; 
      Postcode = postcode; 
      Town = town; 
      Country = country; 
      TwitterUrl = twitterurl; 
      FacebookUrl = facebookurl; 
      GooglePlusUrl = googleplusurl; 
     } 
    } 

創建類型Person的一個實例將是有點乏味,難以閱讀和難以維持:

var person = new Person("Some name", "some postcode", "some town", "some country", "some twitter url", "some fb url", "some g+ url"); 

允許通過簡化其在自己的班級分組相關屬性:

public class Person 
    { 
     public string Name { get; set; } 
     public Address Address { get; set; } 
     public SocialMedia SocialMedia { get; set; } 

     public Person(string name, Address address, SocialMedia socialmedia) 
     { 
      Name = name; 
      Address = address; 
      SocialMedia = socialmedia; 
     } 
    } 

    public class Address 
    { 
     public string Postcode { get; set; } 
     public string Town { get; set; } 
     public string Country { get; set; } 
    } 

    public class SocialMedia 
    { 
     public string TwitterUrl { get; set; } 
     public string FacebookUrl { get; set; } 
     public string GooglePlusUrl { get; set; } 
    } 

現在我們的人只有性,你可以立即看到正在發生的事情。我們已經降低了構造的尺寸更易於管理的狀態:

var address = new Address() { ... }; 
var socialmedia = new SocialMedia() { ... }; 
var person = new Person("Some name", address, socialmedia); 

對於我來說,這是更具可讀性。您的地址&社會化媒體的模型可以在其他地方在應用中重複使用了。

我一直在試圖找出如何序列多個對象 (json.net)到同一個文件,沒有任何運氣了大約一個星期,並有 決定只是儘量保持在一個課堂上的一切。

json.net由Newtonsoft(http://www.newtonsoft.com/json)?如果沒有,那麼我建議你使用這個庫,因爲它已經成爲標準。

序列化我們宣佈person您將需要使用json.net提供了以下方法:

//... 
var person = new Person("Some name", address, socialmedia); 
string personJsonified = JsonConvert.SerializeObject(person); 

,這將產生我們的人的JSON字符串。

但是,你要多個對象。如果你想多Person對象,你可以創建一個列表和序列化:

var person1 = new Person("Jim", address, socialmedia); 
    var person2 = new Person("Bob", address, socialmedia); 
    var people = new List<Person> { person1, person2 }; 

    string peopleJsonified = JsonConvert.SerializeObject(people); 

如果你想在JSON字符串,如人與車要返回多種類型(虛構類中,我們已經創建),你需要一些其他的類來包裝這些了:

  var myPerson = new Person("Jim", address, socialmedia); 
      var myCar = new Car(); 
      var randomObjects = new SomeRandomObjects{ Person = myPerson, Car = myCar }; 

      string randomObjectsJsonified = JsonConvert.SerializeObject(randomObjects); 

希望你會得到你就能從中得到生成您的JSON文件。

也得到任何命名約定的提示?

至於命名約定和理念爲指導,檢查出MSDN Design Guidelines for Developing Class Libraries

+0

當你說要組合這些屬性時,你的意思是使用繼承嗎? 我一直在想如何序列化多個對象(json.net)到同一個文件沒有任何運氣約一個星期,並決定只是試圖保持一切在單一類。在考慮問禮儀的正確問題後,我意識到我已經讓另一個問題在一定程度上導致了一個抽象的問題,爲此我表示歉意。 也有任何命名約定的提示? –

+0

說實話,我在這門課中包含了比我原先想要的更多的屬性,事實上許多屬性都不會經常使用,如果有的話。所以在我解決了我的問題後,我將刪除一些,並希望學習如何將多個對象序列化爲單個文件。 –

+0

@ConorMacF我已經更新了答案,並在評論中回答了您的問題。希望它有一點幫助。我會專注於先讓它工作,然後回到它並重構。無論如何,你似乎已經設置了大部分代碼,所以你幾乎就在那裏。 –