2

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.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
e11s
  • 4,123
  • 3
  • 29
  • 28

2 Answers2

2

This is a common question, please see this post:

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.

Community
  • 1
  • 1
Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
  • 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. – e11s Jan 22 '09 at 20:39
  • 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 Jan 22 '09 at 21:24
  • 1
    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 Jul 25 '09 at 18:48
0

From these comments, it seems that jrista's omment isn't exactly correct: The XmlSerializer classes are dynamically generated, thus the name for classes to add to "InternalsVisibleTo" can't be known up front. Unless you precompile those serializers as explained in the other question.

Community
  • 1
  • 1
tobsen
  • 5,328
  • 3
  • 34
  • 51