在 VB.net 中 XML 序列化友元类
我在 VB.net (2005) 中有一些类(大约 15 个左右),我希望能够将它们序列化为 xml。不幸的是,它们被标记为友元类,不能在程序集之外公开。
该程序集是一个 dll,它是 CAD 系统的 com 互操作插件。我已将所有类设置为朋友,这样它们就不会暴露在程序集之外供第 3 方使用。我想知道我是否需要这样做。将类设置为公共将允许我序列化事物。但是我不希望人们链接到程序集并使用这些类。
我是否应该担心链接到我的程序集的其他程序?事实上我认为这种情况发生的可能性并不大。我只是不喜欢让我的几乎所有课程都具有公共范围的想法。
有没有办法使友元类可序列化?或者我应该把事情公开?
干杯, 特洛伊
I have a few classes (about 15 or so) in VB.net (2005) that I would like to be able to serialize to xml. Unfortunately they are labeled as friend classes and cannot be exposed outside of the assembly.
The assembly is a dll that is a com interop plugin to a CAD system. I have set all of my classes as friends so that they are not exposed outside of the assembly for 3rd party use. I am wondering if I even need to do that. Setting the class to public would allow me to serialize things. However I don't want people linking to the assembly and using the classes.
Should I even worry about other programs linking to my assembly? In fact I don't think there is a large chance of this happening. I just don't like the idea of having almost all of my classes with a public scope.
Is there a way to make a friend class serializable? Or should I just make things public?
Cheers,
Troy
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这种“保护”无论如何也起不了作用。人们可以通过反射加载您的程序集,然后使用这些类。因此,不公开课程的理由就消失了,这简化了事情。
如果您确实想让您的类保持在内部,您仍然可以编写自己的序列化代码(完全手动(高效,但需要大量工作)或通过反射),或者您可以仅出于序列化目的提供单独的仅数据类,这可以公开。然后,您只需编写将内部类中的数据导入/导出到序列化类的代码,并且这些类对于其他开发人员来说没有多大用处,因为实际的代码位于内部类中。
This "protection" won't work anyway. People can load your assembly with reflection and then use the classes. So that reason to not make the classes public vanishes, which simplifies things.
If you really want to keep your classes internal, you can still either write your own serialization code (completely manually (efficient, but much work required) or with reflection), or you can provide separate data-only classes for serialization purposes only, which can be made public. Then you just have to write code that imports/exports the data in your internal classes to the serialization classes, and the classes won't be of much use to other developers since the actual code is in the internal classes.
听起来你是一个受过 Java 培训的人,正在尝试模仿包功能。我做对了吗? :-)
你不应该让你的班级成为朋友。继续将它们公开。如果您希望它们可序列化,那么您实际上是在穿透程序集,这无论如何都会违反友元(包)可见性,这就是它们不可序列化的原因。
如果您希望它们有效地私有但可序列化,请查看 System.Reflection.BindingFlags.NonPublic 标志。您可以使用反射使字段(而不是类)可序列化。
Sounds like you are a java-trained guy who is trying to mimic the package capability. Did I get that right? :-)
You should not make your classes Friend. Go ahead and make them public. If you want them serializable, you are effectively piercing the assembly which violates the Friend (package) visibility anyway and is why they're not serializable.
If you want them to be effectively private but serializable, take a look at the System.Reflection.BindingFlags.NonPublic flag. You can use Reflection to make fields (not classes) serializable.