SGEN XMLSerializer - should be .XMLSerializers.dll added as a reference to the current project or to the GAC?

孤街浪徒 提交于 2019-12-01 16:33:12

问题


I do some stuff with XMLSerializer class. Like a most of beginners I face with performance issue on app start. I read a lot of blogs, articles and finally use SGEN tool. Now performance looks to be ok but few things are still not clear for me.

1) Should I use SGEN syntax like this:

SGen.exe /assembly:MyAssembly /type:MyRootXmlType

or it's enough to just (I use this syntax currently, I have only one Serializable class in my assembly):

SGen.exe /assembly:MyAssembly

When /type parameter can be useful in practice?

2) I read on MSDN ( http://msdn.microsoft.com/en-us/library/ee704594.aspx )

This command generates the serializer assembly MyAssembly.XmlSerializers.dll, which should be added as a reference to the current project or to the GAC.

but on all other blogs and articles (for example on SO) I read that is enough to just put MyAssembly.XmlSerializers.dll into folder where MyAssembly exists.

So what is right?

3) It's enough to just generate .XmlSerilizers.dll and deploy it together with MyAssembly or should I add something to my code yet?

I don't think so but I would like to be sure that I will not have to change my code and "magic" happens automatically.


回答1:


If you don't use the /type argument for sgen.exe then it will generate de/serialization code for all public types in the assembly. Note that the [Serializable] attribute is not used in XML serialization. I doubt you'd want this, use /type to keep the generated assembly small.

Adding a reference is not necessary, Xml serialization always tries an Assembly.Load() on the .XmlSerializers.dll assembly anyway. Plus, you'll never reference the generated XmlSerializationWriterXxx and XmlSerializationReaderXxx classes directly in your code. It does have one advantage, the build system will automatically copy the assembly when you include the project in a solution.

Installing it in the GAC is only worth considering when different apps serialize and deserialize the XML file. You can provide other apps with the .XmlSerializers.dll assembly by copying the assembly by hand as well. Which is a bit error prone, use your own judgment here. Check the previous paragraph for a way to automate the copy.



来源:https://stackoverflow.com/questions/2543641/sgen-xmlserializer-should-be-xmlserializers-dll-added-as-a-reference-to-the-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!