如果属性是显示成员,为什么它必须是公共的
我有一个类,其对象属性标记为“内部”。不存在可访问性问题,整个项目的所有成员都可以访问它们。但是,如果我将任何这些属性设置为任何 GUI 组件的 DisplayMember,则必须将其标记为“公共”才能正常工作。否则,例如列表框,将对象的 ToString() 显示为项目。为什么会这样呢?
I have class with its object properties marked "internal". There is no accessibility issues and across the project all members can access them. But if I have set any of those properties to DisplayMember of any GUI component, then it has to be marked "public" to work properly. Else, say a listbox, displays the object's ToString() as the items. Why is it so?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
内置的数据绑定功能可能仅反映公共属性。
Likely the databinding functionality built in, only reflects on public properties.
内部关键字的工作方式如下:
Internal 关键字的主要目的是让当前程序集中的每个类都可以访问它。例如,如果您有一个 .dll 文件并将其某些类标记为内部,则只有该 .dll 中的类可以访问它,而其他外部程序集或外部程序都不能访问它。如果您在程序集外部使用 GUI,那么您就违反了内部的用途。因此,您必须将该属性成员公开。
虽然我不是 C# 专家,但因为我是一名学生,我建议有一个“连接器”类,用于连接需要访问数据的外部成员,以及用于设置新数据的内部类。
The internal keyword works like so:
The main purpose of the internal keyword is to let every class in your current assembly to access it. For example, if you have a .dll file and mark some of its classes internal, only the classes in that .dll can access it, and no other outside assembly or external program can access it. If you are using a GUI outside of the assembly, then you have just violated what internal is used for. Therefore, you have to make that property member public.
Though I am not an expert in c#, because I am a student, I would suggest having a "connector" class that serves as a connection to the outside members that need to access data, and the internal classes to set new data.