首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >C#类索引器设置字典not属性

C#类索引器设置字典not属性
EN

Stack Overflow用户
提问于 2011-08-24 09:46:45
回答 3查看 382关注 0票数 1

我正在尝试为一个C#类实现一个字符串索引器,但是当你设置一个属性时,字典会被设置,而不是属性。这可能是我遗漏了一些简单的东西,我就是看不出来。

代码语言:javascript
运行
复制
objFiveProp temp = new objFiveProp();
temp["index1"] = 3;

将temp._items"index1".value设置为3。

类:

代码语言:javascript
运行
复制
public class objFiveProp
{

    #region Properties
    private Dictionary<string, int> _items;
    public int this[string key]
    {
        get { return _items[key]; }
        set { _items[key] = value; }
    }

    public int index1 { get; set; }
    public int index2 { get; set; }
    public int index3 { get; set; }
    public int index4 { get; set; }
    public int index5 { get; set; }

    #endregion
    #region Constructor

    public objFiveProp()
    {
        index1 = 0;
        index2 = 0;
        index3 = 0;
        index4 = 0;
        index5 = 0;
        _items = new Dictionary<string, int>();
        _items.Add("index1", index1);
        _items.Add("index2", index2);
        _items.Add("index3", index3);
        _items.Add("index4", index4);
        _items.Add("index5", index5);

    }

    #endregion

}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-08-24 09:51:19

这就是它的工作原理。Dictionary包含用于设置它的整数的副本-而不是对属性的引用。

我会使用下面这样的东西来解决这个问题:

代码语言:javascript
运行
复制
public class objFiveProp
{
    private Dictionary<string, int> _items;
    public int this[string key]
    {
        get { return _items[key]; }
        set { _items[key] = value; }
    }

    public int Index1 
    {
        get { return this["index1"]; } 
        set { this["index1"] = value; }
    }
    public int Index2
    {
        get { return this["index2"]; } 
        set { this["index2"] = value; }
    }

    // ....

    public objFiveProp()
    {
        _items = new Dictionary<string, int>();
        _items.Add("index1", index1);
        _items.Add("index2", index2);
        _items.Add("index3", index3);
        _items.Add("index4", index4);
        _items.Add("index5", index5);    
    }

#endregion

这会导致您的属性总是提取存储在字典中的值,并保存在那里,因此不会有两个值的副本。

票数 2
EN

Stack Overflow用户

发布于 2011-08-24 09:51:04

int是值类型,而不是引用类型。当您在_items中设置一个值时,它不会设置该属性,即使您最初是从该属性添加的。

来自MSDN

基于值类型的

变量直接包含值。将一个值类型变量分配给另一个值类型变量会复制包含的值。这与引用类型变量的赋值不同,后者复制对对象的引用,但不复制对象本身。

如果你真的需要能够从索引器和属性访问你的数据,最简单的方法之一就是重写你的属性:

代码语言:javascript
运行
复制
public int indexN
{
    get { return _items["indexN"]; }
    set { _items["indexN"] = value; }
}

另一种方法是在索引器的setter中使用反射:

代码语言:javascript
运行
复制
public int this[string key]
{
    get { return _items[key]; }
    set 
    { 
        _items[key] = value; 
        PropertyInfo prop = this.GetType().GetProperty(key);
        if (prop != null)
        {
            prop.SetValue(this, null);
        }
    }
}

但请记住,反射是相对非常S-L-O-W的。

也有其他方法可以完成你想要做的事情,但也许最好的解决方案是根本不做这件事。为您的类选择最好的接口,无论是索引器还是属性,并坚持使用它。您的代码将更易于维护(您不必维护类数据的两个公共接口),并且更具可读性(其他程序员不需要知道索引器和属性是同一件事)。干杯!

票数 0
EN

Stack Overflow用户

发布于 2011-08-24 09:58:09

这是因为在index set方法中设置的是字典项的值

代码语言:javascript
运行
复制
public int this[string key]
    {
        get { return _items[key]; } 
        set { _items[key] = value; } //here you are setting the value of dictionary item not the property
    }

或者,为index1、index2等创建单独的属性,或者在上面的set方法中添加检查,通过设置一个脏的解决方案,以根据键的值设置成员变量的值;类似于:

代码语言:javascript
运行
复制
set {  
   _items[key] = value; 
   if(key == "index1")
         index1 = value;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7169571

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档