将duck-typed结构添加到字典c#

时间:2018-06-08 09:14:02

标签: c# dictionary unity3d duck-typing

我想在字典中添加一个duck-typed结构,但是我收到以下错误:

  

参数2:无法转换为' []选项>'到UnityEngine.Object'

这是我的代码:

public class Dynamic_Interface
{
    private static Dictionary<int, Object> Interface_steps = new Dictionary<int, Object>();

    Dynamic_Interface()
    {
        var sentences = new[] 
        {
            new
            {
                identifier = 1,
                text = "string explain",
                options = new[] 
                {
                    new
                    {
                        next = 2,
                        value = "this is the first option"
                    },
                    new
                    {
                        next = 2,
                        value = "this is the second option"
                    },
                    new
                    {
                        next = 2,
                        value = "this is the third option"
                    },
                    new
                    {
                        next = 2,
                        value = "this is the fourth option"
                    },
                }
            },
            new
            {
                identifier = 2,
                text = "string explain second var",
                options = new[] 
                {
                    new
                    {
                        next = 3,
                        value = "this is the second first option"
                    },
                    new
                    {
                        next = 3,
                        value = "this is the second second option"
                    },
                    new
                    {
                        next = 3,
                        value = "this is the second third option"
                    },
                    new
                    {
                        next = 3,
                        value = "this is the second fourth option"
                    }
                }
            },
        };

        foreach (var sentence_obj in sentences)
        {
            Interface_steps.Add(sentence_obj.identifier, sentence_obj);
        }
    }
}

所以最后我想要一个包含sentences[]中每个对象的字典,其中identifier key作为字典中的名称。

我习惯使用javascript,这是我第一次做c#所以很抱歉初学者的错误。但我似乎无法找到如何让这个工作 如果有什么不清楚让我知道所以我可以澄清..

2 个答案:

答案 0 :(得分:2)

那么,这里发生了什么? 当我们仔细查看错误消息

  

参数2:无法转换为&#39; []选项&gt;&#39;到UnityEngine.Object&#39;

它告诉我们,我们正在尝试将options数组转换为UnityEngine.Object类型。嗯。这很奇怪我们没有用这个Unity无意义来定义我们的词典,那么为什么它使用它而不是C#的Object类?!

嗯,你可以使用其他Unity类,可能有像

这样的东西
using UnityEngine;
您的命名空间中的

。 C#的问题在于它的Object类也驻留在名为System的名称空间中,只能通过System.Object完全识别它。所以,如果你没有

using System;

当您输入UnityEngine.Object时,它会愉快地尝试使用System.Object代替Object。因此会发生这种奇怪的编译器错误。

所以只需使用System.Object,一切都很好正确
好吧。但故事还有更多内容!

C#还为其最常见的类型定义了别名 - 所谓的built-in类型:

bool        System.Boolean
byte        System.Byte
sbyte       System.SByte
char        System.Char
decimal     System.Decimal
double      System.Double
float       System.Single
int         System.Int32
uint        System.UInt32
long        System.Int64
ulong       System.UInt64
object      System.Object
short       System.Int16
ushort      System.UInt16
string      System.String

您会发现小写object只是System.Object的别名。可是等等!不管我们的使用陈述是什么,都不会使object 总是使用System.Object?答案是肯定的,是的,它确实......

让我通过以下示例说明C#中棘手的命名空间实际上是多少:

public class Object
{
    public string Name { get; set; }
}
public class TestConsole
{
    public static void Main(string[] args)
    {
        Object fakeObject = new Object(); // -> Custom Object class
        object realDeal = new object();   // -> System.Object

        string name = fakeObject.Name    // OK
        name = realDeal.Name             // NOT OK
    }
}

这是否意味着在使用System类时我们应该始终使用内置别名?不是真的,但我们应该而不是使用Microsoft使用的命名约定。这意味着无论何时使用像数据类型这样的类,都应该使用内置函数,每当使用类的静态成员时,都应该使用它的全名。例如:

object humberto = "humberto";
string rudolf = "rudolf";
Object.Equals(humberto, rudolf);

答案 1 :(得分:0)

强类型是C#的优点之一,它比鸭式更加冗长,但为您节省了很多麻烦,如果可能的话,试着限制var,它的几个字符更短但是如果你使用强类型则更难不小心要求编译器做一些不同于你真正想要的事情:

似乎立即脱颖而出的替代方法有两种选择

class myClass { int next; string value; };
Dictionary <int, myClass> dict;

或者,虽然我认为这不是你想做的事情

Dictionary<int, Dictionary<int,string>> dict; 

第二个选项不要求您声明类类型,但请记住字典需要唯一键。有点读取你想使用数组索引作为键,但是你仍然使用字典,是否有原因?我的解决方案可能有些偏差,但如果你能解释一下你想要做什么样的查询,我会很乐意提出更多建议。

也许您想将上述内容合并到

Dictionary<int, Dictionary<int,myClass>> dict;