当属性不是默认值时,如何使 Delphi Prism 索引属性对 C# 可见
我有几个带有索引属性的 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);
我不知道我是否遗漏了某些内容,但如果删除属性声明中的 default ,我将看不到 IndexedBool 属性。除此之外,我非常确定直接访问类实例的私有方法是错误的。
有什么想法吗?
I have several Delphi Prism classes with indexed properties that I use a lot on my C# web applications (we are migrating a big Delphi Win32 system to ASP.Net). My problem is that it seems that C# can't see the indexed properties if they aren't the default properties of their classes. Maybe I'm doing something wrong, but I'm completely lost.
I know that this question looks a lot like a bug report, but I need to know if someone else knows how to solve this before I report a bug.
If I have a class like this:
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;
I can use this class in C# like this:
var myObj = new TMyClass();
myObj[0] = true;
However, if TMyClass is defined like this:
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;
Then the IndexedBool property becomes invisible in C#. The only way I can use it is doing this:
var myObj = new TMyClass();
myObj.set_IndexedBool(0, true);
I don't know if I'm missing something, but I can't see the IndexedBool property if I remove the default in the property declaration. Besides that, I'm pretty sure that it is wrong to have direct access to a private method of a class instance.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我相信 C# 4.0 将支持索引属性,但遗憾的是之前的任何内容都不会。
不幸的是,您要求的是 C# 的限制,而不是 Delphi Prism。来自 Delphi Prism 与 C# 上的 Delphi Prism 文档 wiki 页面:
本页还概述了 Delphi Prism 代码包含 C# 上的独特或扩展功能的其他领域,这些功能可能对您的移植有用。
I believe that C# 4.0 will support indexed properties but that anything before that will sadly not.
Unfortunately, what you are asking for is a limitation of C# and Not Delphi Prism. From the Delphi Prism Documentation wiki page on Delphi Prism vs C#:
This page also outlines other areas where Delphi Prism code includes unique or extended features over C# which might be useful in your port.
不幸的是,这是 C# 允许您访问索引属性的唯一方式。这是 C# 编译器的限制(vb.net 应该可以做到)。
It's unfortunate but that's the only way C# lets you access index properties. It's a C# compiler limitation (vb.net should do it fine).
不美观或不干净,但作为一种快速解决方法,您可以将索引属性访问器公开。这样您就可以在 C# 中使用它们来访问这些值。
Not beautiful or clean, but as a fast workaround you could make the indexed property accessors public. That way you can use them from C# to access the values.