C#在另一个字典中使用TValue的字典

C#在另一个字典中使用TValue的字典,c#,generics,dictionary,types,C#,Generics,Dictionary,Types,我对C#相当陌生,而且我有红宝石背景。我还有很多东西要学,这就是为什么我要问以下问题: 目标: 1) 我想创建一个字典,用字符串作为键,用我想要的任何对象类型作为值。大概是这样的: Dictionary<string, T> Dictionary<string, Dictionary<string T>> 当前代码: 我正试图使用以下代码实现这一点 public List<string> DataTypes; public Dictionary

我对C#相当陌生,而且我有红宝石背景。我还有很多东西要学,这就是为什么我要问以下问题:

目标: 1) 我想创建一个字典,用字符串作为键,用我想要的任何对象类型作为值。大概是这样的:

Dictionary<string, T>
Dictionary<string, Dictionary<string T>>
当前代码: 我正试图使用以下代码实现这一点

public List<string> DataTypes;
public Dictionary<string, Dictionary<string, object>> TempData;
public Dictionary<string, Dictionary<string, object>> GameData;

public Session()
        {   
            // Create a list of all Data Types.
            DataTypes = new List<string>();
            DataTypes.Add("DataInfo");
            DataTypes.Add("Maps");
            DataTypes.Add("Tilesets");

            // Create and populate TempData dictionary.
            TempData = new Dictionary<string, Dictionary<string, object>>();
            TempData.Add("DataInfo", new Dictionary<string, DataInfo>());
            TempData.Add("Maps", new Dictionary<string, Map>());
            TempData.Add("Tilesets", new Dictionary<string, Tileset>());

            // Create GameData dictionary and copy TempData into it.
            GameData = new Dictionary<string, object>(TempData);
        }
公共列表数据类型;
公共数据字典;
公共数据字典;
公开会议()
{   
//创建所有数据类型的列表。
数据类型=新列表();
数据类型。添加(“数据信息”);
数据类型。添加(“地图”);
数据类型。添加(“Tilesets”);
//创建并填充临时数据字典。
TempData=新字典();
Add(“DataInfo”,newdictionary());
Add(“Maps”,newdictionary());
Add(“Tilesets”,newdictionary());
//创建GameData字典并将TempData复制到其中。
GameData=新字典(TempData);
}
问题: 我得到以下错误

1) 与“System.Collections.Generic.Dictionary>.Add(string,System.Collections.Generic.Dictionary)”匹配的最佳重载方法具有一些无效参数

2) 错误10参数1:无法从“System.Collections.Generic.Dictionary>”转换为“System.Collections.Generic.IDictionary”

以下几行用红色下划线

TempData.Add("DataInfo", new Dictionary<string, DataInfo>());
TempData.Add("Maps", new Dictionary<string, Map>());
TempData.Add("Tilesets", new Dictionary<string, Tileset>());

// Create GameData dictionary and copy TempData into it.
GameData = new Dictionary<string, object>(TempData);
TempData.Add(“DataInfo”,newdictionary());
Add(“Maps”,newdictionary());
Add(“Tilesets”,newdictionary());
//创建GameData字典并将TempData复制到其中。
GameData=新字典(TempData);
很明显,我在这里做错了什么,甚至是违法的,我只需要知道它是什么,以及如何修复它

我自己做了很多研究,但在这方面没有发现任何可以帮助我的东西。 我已经看过如何制作字典,但我不太确定如何告诉字典不要关心值中的对象类型

我知道“T”在这里可能有一些用处,但我真的不知道如何使用它,因为它总是告诉我“找不到类型或命名空间名称“T”

那我该怎么办

提前谢谢
-萨沙

你可以把所有的口述都改成字符串、对象

但也许,因为你来自Ruby,所以还有另一个问题

把所有的东西都放到字典里,然后在提取的时候再计算出它们是什么类型,这也许不是最好的方法。在类型化语言中,通常最好为所有内容创建类型

也许最好有一个类型化对象和它们所属类型的字典

class GameResources
{
    public Dictionary<string, Map> Maps { get; set;}
    public Dictoinary<string, Tileset> Tileset { get; set; }
}
类游戏资源
{
公共字典映射{get;set;}
公共听写Tileset{get;set;}
}

等等。。。或者很可能经过一点思考,另一个类结构可能更适合您的情况

问题在于您没有将typedef与您创建的对象相匹配。将代码更改为:

public void Session()
{
    // Create a list of all Data Types.
    DataTypes = new List<string>();
    DataTypes.Add("DataInfo");
    DataTypes.Add("Maps"); 
    DataTypes.Add("Tilesets");

    // Create and populate TempData dictionary.
    TempData = new Dictionary<string, Dictionary<string, object>>();
    TempData.Add("DataInfo", new Dictionary<string, object>());
    TempData.Add("Maps", new Dictionary<string, object>());
    TempData.Add("Tilesets", new Dictionary<string, object>());

    // Create GameData dictionary and copy TempData into it.
    GameData = new Dictionary<string, Dictionary<string, object>>(TempData);
}
公共作废会话()
{
//创建所有数据类型的列表。
数据类型=新列表();
数据类型。添加(“数据信息”);
数据类型。添加(“地图”);
数据类型。添加(“Tilesets”);
//创建并填充临时数据字典。
TempData=新字典();
Add(“DataInfo”,newdictionary());
Add(“Maps”,newdictionary());
Add(“Tilesets”,newdictionary());
//创建GameData字典并将TempData复制到其中。
GameData=新字典(TempData);
}

听起来很有趣。与AresAvatar方法相比有什么好处?您保留了所有类型信息供初学者使用!看起来您正在制作一个“固定”结构的字典来访问地图、瓷砖集等。这些都是游戏中的一流概念,所以最好将它们保留为类型。AresAvatar(这是解决您眼前问题的简单方法)将要发生的事情是,当您重新获取对象时,您将不得不将它们转换回其数据类型。您的代码将隐式了解字典键中的类型。避免这种情况,创建类型,无需铸造!
public void Session()
{
    // Create a list of all Data Types.
    DataTypes = new List<string>();
    DataTypes.Add("DataInfo");
    DataTypes.Add("Maps"); 
    DataTypes.Add("Tilesets");

    // Create and populate TempData dictionary.
    TempData = new Dictionary<string, Dictionary<string, object>>();
    TempData.Add("DataInfo", new Dictionary<string, object>());
    TempData.Add("Maps", new Dictionary<string, object>());
    TempData.Add("Tilesets", new Dictionary<string, object>());

    // Create GameData dictionary and copy TempData into it.
    GameData = new Dictionary<string, Dictionary<string, object>>(TempData);
}