C# 参数化属性类通过泛型实现?
我想构建一个通用类,可用于在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你想要这样的东西:
You want something along these lines:
由于您显然不关心将特定业务逻辑附加到此索引属性的 getter 或 setter(就像您可以使用 C# 中的默认索引器或 VB.NET 中的任何属性一样),因此您需要一个内部存储机制,以便完成这个。
但这实际上只是为您提供了一个类似于字典的构造,但在索引不存在时不会抛出异常。
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.
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.