Class Object返回覆盖的Dictionary< string,object> [英] Class Object return overrided Dictionary<string, object>

查看:68
本文介绍了Class Object返回覆盖的Dictionary< string,object>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码:

I have this code:

public class DB
{
public Dictionary<string,object> DATA = new Dictionary<string,object>();
public DB()
{
this.DATA = new Dictionary<string,object>();
}
public override string ToString()
{
    return DATA.Count.ToString();
}
}



我想覆盖字典< string,object> as对象就像覆盖字符串ToString(),如下面的代码:


I wanting to override the Dictionary<string,object> as object just like the "override string ToString()" like the codes below:

void main()
{
DB value = new DB();
value.Add("Attr1", new ClassA());
value.Add("Attr2", new ClassB());
value["Attr1"] = new ClassC();
}





但是现在,我只能做到这一点而不是以上:



But for now, I only able to do this instead of above:

void main()
{
DB value = new DB();
value.DATA.Add("Attr1", new ClassA());
value.DATA.Add("Attr2", new ClassB());
value.DATA["Attr1"] = new ClassC();
}







是否有任何解决方案或类似的解决方案?




Is there any solution to that or similar to it?

推荐答案

好吧,你不能覆盖一个字段。但我认为你想要一个索引属性 - 但你仍然需要实现Add()方法。看看这里: https://msdn.microsoft.com/en -us / library / aa288464%28v = vs.71%29.aspx [ ^ ]。



[Update1]

如果你只需要使用ToString,只需覆盖它:

Well, you can't override a field. But I think you want an indexed property - but you will still need to implement the Add() methods. Take a look here: https://msdn.microsoft.com/en-us/library/aa288464%28v=vs.71%29.aspx[^].

[Update1]
If yor only need is to oveddide ToString, override it simply:
public class DB : Dictionary<string,object>
{
public override string ToString()
{
    return this.Count.ToString();
}
}



[Update2]

了解在这种情况下如何使用真正的封装和索引属性。完全按照您的意愿:


[Update2]
See how real encapsulation and index property could be used in this case. Exactly as you wanted:

public class DB
{
    private Dictionary<string,object> DATA = new Dictionary<string,object>();
    public DB()
    {
        this.DATA = new Dictionary<string,object>();
    }

    public string ToString()
    {
        return DATA.Count.ToString();
    }

    public void Add(string index, object value)
    {
        DATA.Add(index, value);
    }

    public object this[string index]
    {
        get
        {
            if(DATA.ContainsKey(index))
            {
                return DATA[index];
            }
            else
            {
                return null;
            }
        }
        set
        {
            if(DATA.ContainsKey(index))
            {
                DATA[index] = value;
            }
            else
            {
                DATA.Add(index, value);
            }
        }
    }
}

void Main()
{
    DB value = new DB();
    value.Add("Attr1", 1);
    value.Add("Attr2", 2);
    value["Attr1"] = 3;
    value["Attr3"] = 4;

    Console.WriteLine(value.ToString()); // 3
    Console.WriteLine(value["Attr1"]);// 3
    Console.WriteLine(value["Attr2"]);// 2
    Console.WriteLine(value["Attr3"]);// 4
    Console.WriteLine(value["Attr4"]);// null
}


使用e xtend



use extend

public class DB 
{
public Dictionary<string,object> DATA = new Dictionary<string,object>();
public DB()
{
this.DATA = new Dictionary<string,object>();
}
public override string ToString()
{
    return DATA.Count.ToString();
}
}







to:






to:

public class DB : Dictionary<string,object>
{
public Dictionary<string,object> DATA = new Dictionary<string,object>();
public DB()
{
this.DATA = new Dictionary<string,object>();
}
public override string ToString()
{
    return DATA.Count.ToString();
}
}


这篇关于Class Object返回覆盖的Dictionary&lt; string,object&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆