我有几个带有索引属性的Delphi Prism类,我在C#Web应用程序中使用了很多(我们正在将一个大型Delphi Win32系统迁移到ASP.Net)。我的问题是,如果C#不是它们类的默认属性,它似乎看不到索引属性。也许我做错了什么,但我完全迷失了。
我知道这个问题看起来很像错误报告,但在报告错误之前,我需要知道其他人是否知道如何解决这个问题。
如果我有这样的课程:
TMyClass = public class
private
...
method get_IndexedBool(index: Integer): boolean;
method set_IndexedBool(index: Integer; value: boolean);
public
property IndexedBool[index: Integer]: boolean
read get_IndexedBool
write set_IndexedBool; default; // make IndexedBool the default property
end;
我可以在C#中使用这个类:
var myObj = new TMyClass();
myObj[0] = true;
但是,如果TMyClass定义如下:
TMyClass = public class
private
...
method get_IndexedBool(index: Integer): boolean;
method set_IndexedBool(index: Integer; value: boolean);
public
property IndexedBool[index: Integer]: boolean
read get_IndexedBool
write set_IndexedBool; // IndexedBool is not the default property anymore
end;
然后,IndexedBool属性在C#中变得不可见。我可以使用它的唯一方法就是这样做:
var myObj = new TMyClass();
myObj.set_IndexedBool(0, true);
我不知道我是否遗漏了某些内容,但如果删除属性声明中的默认,则无法看到IndexedBool属性。除此之外,我很确定直接访问类实例的私有方法是错误的。
有什么想法吗?
答案 0 :(得分:1)
我相信C# 4.0 will support索引属性,但在此之前的任何事情都可能不会。
不幸的是,你要求的是C#和非Delphi Prism的限制。来自Delphi Prism vs C#上的Delphi Prism Documentation wiki页面:
C#只能访问默认的索引属性。在Delphi Prism中,您可以使用其名称定义和使用其他索引属性。
此页面还概述了Delphi Prism代码包含C#的唯一或扩展功能的其他区域,这些功能在您的端口中可能很有用。
答案 1 :(得分:0)
这很不幸,但这是C#允许您访问索引属性的唯一方法。这是一个C#编译器限制(vb.net应该做得很好)。
答案 2 :(得分:0)
不漂亮或干净,但作为快速解决方法,您可以将索引属性访问者公开。这样您就可以使用C#来访问这些值。