【问题标题】:Non-generic indexer for C# COM DLL propertyC# COM DLL 属性的非通用索引器
【发布时间】:2016-07-10 03:57:03
【问题描述】:

我正在尝试创建一个具有可在 Microsoft Access 中公开的索引的对象数组。我已经找到了如何创建索引器类,但由于它是通用的,因此该属性不会在 Access VBA 中公开。更具体地说,我正在转换一个可与 Access 一起使用的 VB.NET COM DLL,这是我要转换的字符串数组属性代码:

    Public Shared _ReportParameters(0 To 9) As String
    Public Property ReportParameters(Optional index As Integer = Nothing) As String
        Get
            Return _ReportParameters(index)
        End Get

        Set(ByVal Value As String)
            _ReportParameters(index) = Value
        End Set
    End Property

这是我已转换为 C# 的代码,它使用了无法在 DLL 中公开的索引器类:

    public static string[] _ReportParameters = new string[10];
    public Indexer<string> ReportParameters
    {
        get
        {
            return _ReportParameters.GetIndexer();
        }
    }

有什么想法吗?

【问题讨论】:

    标签: c# vb.net vba generics dll


    【解决方案1】:

    您发布的 VB.NET 代码中最接近的 C# 代码如下:

    class Thing
    {
        public static string[] _ReportParameters = new string[10];
        public string[] ReportParameters { get { return _ReportParameters; } }
    }
    

    用作

    var thing = new Thing();
    var result = thing.ReportParameters[0];
    thing.ReportParameters[1] = "Test";
    

    但是,索引器是这样写的:

    class Thing
    {
        public static string[] _ReportParameters = new string[10];
        public string this[int index]
        {
            get
            {   
                return _ReportParameters[index];
            }
            set
            {
                _ReportParameters[index] = value;
            }
        }
    }
    

    用作

    var thing = new Thing();
    var result = thing[0];
    thing[1] = "Test";
    

    【讨论】:

    • 另外我应该注意,实例属性返回静态(和公共)字段有点奇怪。
    猜你喜欢
    • 2013-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-22
    • 1970-01-01
    • 1970-01-01
    • 2011-09-21
    • 1970-01-01
    相关资源
    最近更新 更多