views:

501

answers:

1

Is it possible to make XmlSerializer serialize internal class members by using InternalsVisibleTo attribute?

If it is, what assembly should I make my internals visible to. In other words, what assembly name and public key should I provide to the InternalsVisibleTo attribute.

+2  A: 

This is a common question, please see this post:

http://stackoverflow.com/questions/420662/can-an-internal-setter-of-a-property-be-serialized

The DataContractSerializer will let you serialize any members you want. As it is an opt-in method of serialization you will need to annotate the class as needed.

Edit

After re-reading your question, DataContractSerializer may work but that may not be what you want to do. The XMLSerializer will work with InternalsVisibleTo as it will be able to see those members but I would recommend that you look at DataContractSerializer as it is (in my opinion) a better serializer.

Andrew Hare
I assumed it should work as well, but after adding [assembly: InternalsVisibleTo("System.Xml")] to my assembly, internals still not being serialized.I am not using DataContractSerializer because my xml contains attributes, and as far as I know DataContractSerializer only works with elements.
moose-in-the-jungle
Oh - I thought you were trying to have one assembly serialize another assembly's internal members - setting your assembly to be visible to System.Xml will unfortunately not work. Take a look at DataContractSerializer, it should be able to do what you want.
Andrew Hare
InternalsVisibleTo is really only useful when the friend assembly directly references internal types or members. Since System.Xml is not dependent upon your assembly, using InternalsVisibleTo will have no effect.
jrista