C# 参数化属性类通过泛型实现?

发布于 2024-09-06 04:36:43 字数 1499 浏览 9 评论 0原文

我想构建一个通用类,可用于在 C# 中实现参数化属性。我想要的基本代码是这样的:

public class ParameterizedProperty<typeReturn, typeIndexer> {
    private typeReturn oReturn = default(typeReturn);

    public typeReturn this[typeIndexer oIndexer] {
        get { return oReturn[oIndexer]; }
        set { oReturn[oIndexer] = value; }
    }
}

但是由于 typeReturn 未知/不受限制,编译器正在 oReturn[oIndexer] 行上绘制。

那么我该如何让它发挥作用呢?我的第一个(好吧,第二个)想法是使用反射来有条件地查找索引器(如果找不到索引器,则会引发错误),或者可能使用 where typeReturn: I??? 修饰符类定义。但如果我使用修饰符,I??? 会是什么?

感谢您提供此问题的任何解决方案!

注意:如何通过反射正确找到索引器(据说)可以在这里找到:http ://www.dotnet247.com/247reference/msgs/3/16665.aspx(请参阅最后一条消息)。

编辑:正如马修在下面指出的那样...我在原始描述中存在一些拼写错误/错误...我自己的错,真的...最初是在睡觉前写的!我想要的代码看起来更像是这样的:

public class ParameterizedProperty<typeReturn, typeIndexer> {
    private typeReturn[] oReturn;

    public ParameterizedProperty(typeReturn[] oSource) {
        oReturn = oSource;
    }

    public typeReturn this[typeIndexer oIndexer] {
        get { return oReturn[oIndexer]; }
        set { oReturn[oIndexer] = value; }
    }
}

...你敢打赌它会起作用吗!?

呃,不完全是!仍然抱怨 typeIndexer (想要一个“int-able”类型),但更好了!我还添加了一个构造函数来解决那个讨厌的“我实际上无法将 oReturn 设置为任何内容”问题 =)

但即使这样看起来也不太正确(数组总是由 INT 索引,对吧?如果是这样,就不需要 typeIndexer )...明天上班时我必须再看一遍=)

I would like to build a generic class that I can use to implement Parameterized Properties in C#. The basic code I want is this:

public class ParameterizedProperty<typeReturn, typeIndexer> {
    private typeReturn oReturn = default(typeReturn);

    public typeReturn this[typeIndexer oIndexer] {
        get { return oReturn[oIndexer]; }
        set { oReturn[oIndexer] = value; }
    }
}

But since typeReturn is unknown/unrestricted the compiler is plotzing on the oReturn[oIndexer] lines.

So how do I get this to work? My first (well, second) thought is to use Reflection in order to conditionally find the indexer (and raise an error if one is not found) or maybe use the where typeReturn: I??? modifier on the class definition. But if I use the modifier, what would I??? be?

Thanks for any solutions to this problem!

NOTE: How to find the Indexer correctly via Reflection is (supposedly) found here: http://www.dotnet247.com/247reference/msgs/3/16665.aspx (see last message).

EDIT: As Matthew pointed out below... I've got something of a typo/bug in the original description... My own fault, really... originally writing this just before bed! The code I want would look more like this:

public class ParameterizedProperty<typeReturn, typeIndexer> {
    private typeReturn[] oReturn;

    public ParameterizedProperty(typeReturn[] oSource) {
        oReturn = oSource;
    }

    public typeReturn this[typeIndexer oIndexer] {
        get { return oReturn[oIndexer]; }
        set { oReturn[oIndexer] = value; }
    }
}

...and what'll you'll bet that works!?

Er, not quite! Still complaining about typeIndexer (wanting an "int-able" type), but much more betterer! I also added in a constructor to solve that pesky "I can't actually set oReturn to anything" problem =)

But even this doesn't look quite right (Arrays are always indexed by INTs, right? if so there's no need for typeIndexer)... I'll have to look at it again at wurk tomorrow =)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

昨迟人 2024-09-13 04:36:43

你想要这样的东西:

public class ParameterizedProperty<typeReturn, typeIndexer> {
    private Dictionary<typeIndexer, typeReturn> oReturn = new Dictionary<typeIndexer, typeReturn>();

    public typeReturn this[typeIndexer oIndexer] {
        get { return oReturn[oIndexer]; }
        set { oReturn[oIndexer] = value; }
    }
}

You want something along these lines:

public class ParameterizedProperty<typeReturn, typeIndexer> {
    private Dictionary<typeIndexer, typeReturn> oReturn = new Dictionary<typeIndexer, typeReturn>();

    public typeReturn this[typeIndexer oIndexer] {
        get { return oReturn[oIndexer]; }
        set { oReturn[oIndexer] = value; }
    }
}
日久见人心 2024-09-13 04:36:43

由于您显然不关心将特定业务逻辑附加到此索引属性的 getter 或 setter(就像您可以使用 C# 中的默认索引器或 VB.NET 中的任何属性一样),因此您需要一个内部存储机制,以便完成这个。

public class ParameterizedProperty<TReturn, TIndexer>
{
    private Dictionary<TIndexer, TReturn> storage = new Dictionary<TIndexer, TReturn>();

    public TReturn this[TIndexer index]
    {
        get
        {
            TReturn output;

            storage.TryGetValue(index, out output);

            return output;
        }
        set { storage[index] = value; }
    }
}

但这实际上只是为您提供了一个类似于字典的构造,但在索引不存在时不会抛出异常。

Since you're evidently not concerned about attaching specific business logic to the getter or setter for this indexed property (like you could do with the default indexer in C# or any property in VB.NET), you need an internal storage mechanism in order to accomplish this.

public class ParameterizedProperty<TReturn, TIndexer>
{
    private Dictionary<TIndexer, TReturn> storage = new Dictionary<TIndexer, TReturn>();

    public TReturn this[TIndexer index]
    {
        get
        {
            TReturn output;

            storage.TryGetValue(index, out output);

            return output;
        }
        set { storage[index] = value; }
    }
}

But all this really does is provide you with a construct similar to a Dictionary but doesn't throw an exception when an index isn't present.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文