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,);
如果我沒有足夠的提供/右信息,請不要猶豫,說白了!
附帶事情: 我認爲我會像添加其他數組一樣添加到列表中? 這是一個完全可怕的方式嗎?
'的IList'是接口,使用'列表'如'的IList 塊=新列表()' –
參見[對象和在C#集合初始化(https://msdn.microsoft.com/en-us/library/bb384062.aspx) –