Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#中的Luatable等价物?_C# - Fatal编程技术网

C#中的Luatable等价物?

C#中的Luatable等价物?,c#,C#,我一直在寻找一种方法来制作C#(3.5)中类似桌子的东西,但结果仍然是空的。基本上我想这样做 var myClassVar = new myClass(); myClassVar["hello"]["world"] = "hello world"; myClassVar["hello"][0] = "swapped"; myClassVar[0][0] = "test"; myClassVar["hello"]["to"]["the"]["world"]

我一直在寻找一种方法来制作C#(3.5)中类似桌子的东西,但结果仍然是空的。基本上我想这样做

    var myClassVar = new myClass();
    myClassVar["hello"]["world"] = "hello world";
    myClassVar["hello"][0] = "swapped";
    myClassVar[0][0] = "test";
    myClassVar["hello"]["to"]["the"]["world"] = "again";
    myClassVar[0][1][0][0] = "same as above sorta";
我试图创建这种类型的类来解析我为存储数据而创建的文件格式。有人知道这样的事吗

public class LuaTable
{
    private Dictionary<object, dynamic> properties = new Dictionary<object, dynamic>();
    public dynamic this[object property]
    {
        get
        {
            if (properties.ContainsKey(property))
                return properties[property];
            LuaTable table = new LuaTable();
            properties.Add(property, table);
            return table;
        }
        set
        {
            if (!properties.ContainsKey(property))
                properties.Add(property, value);
            else
                properties[property] = value;
        }
    }
}
Edit:我刚刚意识到C#3.5没有
动态
,所以这里有一个简单使用泛型的版本。我希望这样就可以了(因为您确实必须让表的所有子属性都是相同的类型(在本例中,
string
):

公共类LuaTable,其中T:class
{
私有布尔值;
私有T值=null;
私有字典属性=新字典();
公共静态隐式运算符LuaTable(T val)
{
如果(val可计算)
返回(LuaTable)val;
返回新的LuaTable(){isValue=true,value=val};
}
公共静态隐式运算符T(LuaTable表)
{
if(表.isValue)
返回表.value;
返回表;
}
公共LuaTable此[对象属性]
{
得到
{
如果(isValue)
返回null;
if(properties.ContainsKey(property))
归还财产[财产];
LuaTable table=新LuaTable();
添加(属性,表);
返回表;
}
设置
{
如果(!properties.ContainsKey(property))
添加(属性、值);
其他的
属性[属性]=值;
}
}
}
要使用它,它几乎与上面完全相同。只需更改第一行:

var myClassVar = new LuaTable<string>();
var myClassVar=new LuaTable();

你可能想重新考虑你的设计。也不要用“我创建文件格式”考虑使用像JSON这样的现有格式。只是在C 3.5中用“对象”作为表的类型进行测试,这样它就可以保存任何东西。工作太棒了!我唯一的问题是我不能制造[这个]。“那”]并检索它,我收到一个NullReferenceException,代码如下:
var myClassVar=new LuaTable();myClassVar[“hello”][“world”]=“hello”;MessageBox.Show((string)myClassVar[“hello”][“world”]);MessageBox.Show((string)myClassVar[“hello”[“world”]);
@user2377834可能我误解了Lua表是什么(实际上,我唯一的参考是你问题中给出的示例),但在我看来,如果你说
myTable[“hello”][“world”]=“hello”
,那么
myTable[“hello”]
应该是一个表,而不是一个字符串(因此,在MessageBox中显示它不应该显示任何有趣的内容,也不应该使用字符串)。Ehrm,你是对的。嘎,我现在意识到我做了什么,我觉得自己很傻。哈哈,顺便说一句,再次感谢你!)@用户2377834没问题!我很高兴我能够编写一个有趣的类,并在此过程中帮助他人。:)
public class LuaTable<T> where T : class
{
    private bool isValue;
    private T value = null;
    private Dictionary<object, LuaTable<T>> properties = new Dictionary<object, LuaTable<T>>();

    public static implicit operator LuaTable<T>(T val)
    {
        if (val is LuaTable<T>)
            return (LuaTable<T>)val;
        return new LuaTable<T>() { isValue = true, value = val };
    }
    public static implicit operator T(LuaTable<T> table)
    {
        if (table.isValue)
            return table.value;
        return table;
    }

    public LuaTable<T> this[object property]
    {
        get
        {
            if (isValue)
                return null;

            if (properties.ContainsKey(property))
                return properties[property];
            LuaTable<T> table = new LuaTable<T>();
            properties.Add(property, table);
            return table;
        }
        set
        {
            if (!properties.ContainsKey(property))
                properties.Add(property, value);
            else
                properties[property] = value;
        }
    }
}
var myClassVar = new LuaTable<string>();