views:

253

answers:

2

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

+1  A: 

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.

Rap
You might also want to make them invisible through COM using the ComVisibleAttribute?
MarkJ
Nope - not java trained - just coming from vb6 :)
Bluebill
+1  A: 

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.

OregonGhost
That is a good point about reflection. I think I will make my classes public (as that is the default in VS 2005 anyway).
Bluebill