命名在C#中的索引属性? [英] Named indexed property in C#?

查看:146
本文介绍了命名在C#中的索引属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几种语言 - 像德尔福 - 具有创建索引的一个非常方便的方法:不仅全班,但即使是单一的属性可以被索引,比如:

A few languages - like Delphi - has a very convenient way of creating indexers: not only the whole class, but even single properties can be indexed, for instance:

type TMyClass = class(TObject)
protected
    function GetMyProp(index : integer) : string;
    procedure SetMyProp(index : integer; value : string);
public
    property MyProp[index : integer] : string read GetMyProp write SetMyProp;
end;

这可以很容易地使用:

var c : TMyClass;

begin
    c = TMyClass.Create;
    c.MyProp[5] := 'Ala ma kota';
    c.Free;
end;



有没有办法实现在C#中容易吗?

Is there a way to achieve the same effect in C# easily?

推荐答案

众所周知的解决方案是创建一个代理类:

The well-known solution is to create a proxy class:

public class MyClass
{
    public class MyPropProxy
    {
        private MyClass c;

        // ctor etc.

        public string this[int index]
        {
            get
            {
                return c.list[index];
            }
            set
            {
                c.list[index] = value;
            }
        }
    }

    private List<string> list;
    private MyPropProxy myPropProxy;

    // ctor etc.

    public MyPropProxy MyProp
    { 
        get
        {
            return myPropProxy;
        }
    }
}



但(有例外,那这实际上解决了这个问题),这种解决方案大多只能缺点:

But (with exception, that this actually solves the problem), this solution introduces mostly only cons:


  • 这导致代码以通过(可能)有很多被污染小代理类。

  • 主办解决方案打破了封装的一点点的(内部类访问外部类的私有成员),一个更好的将通过列表的实例 MyPropProxy 构造函数,你会需要更多的代码。

  • 揭露内部辅助类是不是我会建议。有人可能会解决通过引入额外的接口,但是这甚至多一个实体来实现(测试,维护等)

  • It causes the code to be polluted by (possibly) a lot of small proxy classes.
  • Presented solution breaks encapsulation a little (inner class accesses private members of the outer class), a better one would pass an instance of list to MyPropProxy's ctor, what would require even more code.
  • Exposing internal helper classes is not something I would suggest. One may solve that by introducing additional interface, but that's even one more entity to implement (test, maintain etc.)

有另一种方法,虽然。它也污染了代码一点,但肯定少了很多,比前一个:

There's another way, though. It also pollutes the code a little, but surely a lot less, than the previous one:

public interface IMyProp
{
    string this[int index] { get; }
}

public class MyClass : IMyProp
{
    private List<string> list;

    string IMyProp.this[int index]
    {
        get
        {
            return list[index];
        }
        set
        {
            list[index] = value;
        }
    }

    // ctor etc.

    public IMyProp MyProp 
    {
        get
        {
            return this;
        }
    }
}



优点:

Pros:


  • 无代理类(占用的内存空间,服务只有一个目的,(最简单的解决方案)打破封装。

  • 简单的解决方案,只需要很少的代码添加其他索引属性

缺点:


  • 每个属性需要不同的公共接口

  • 随着索引属性的增加,类实现更多接口

这是最简单的(代码长度和复杂性方面)引入索引属性到C#的方式。除非有人张贴更短和更简单的之一,当然。

This is the simplest (in terms of code length and complexity) way of introducing indexed properties to C#. Unless someone posts even shorter and simpler one, of course.

这篇关于命名在C#中的索引属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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