It is mentioned -- "To increase performance, the XML serialization infrastructure dynamically generates assemblies to serialize and deserialize specified
types."
What means the assemblies?
2.
For the following sample, why creates two XMLSerializer instances, one is s and the other is ser?
Hashtable serializers = new Hashtable();
// Use the constructor that takes a type and XmlRootAttribute.
XmlSerializer s = new XmlSerializer(typeof(MyClass), myRoot);
// Implement a method named GenerateKey that creates unique keys
// for each instance of the XmlSerializer. The code should take
// into account all parameters passed to the XmlSerializer
// constructor.
object key = GenerateKey(typeof(MyClass), myRoot);
// Check the local cache for a matching serializer.
XmlSerializer ser = (XmlSerializer)serializers[key];
if (ser == null)
{
ser = new XmlSerializer(typeof(MyClass), myRoot);
// Cache the serializer.
serializers[key] = ser;
}
else
{
// Use the serializer to serialize, or deserialize.
}
thanks in advance,
George
Shuja Ali
May 13th, 2008, 01:39 PM
It is mentioned -- "To increase performance, the XML serialization infrastructure dynamically generates assemblies to serialize and deserialize specified
types."This means an assembly is generated at the run-time which actually converts the Object into an XML stream. Read the whole article and you will understand what it means.
What means the assemblies?
Assemblies in .NET on Wiki (http://en.wikipedia.org/wiki/.NET_assembly)
Look at the code carefully and see what exactly is happening. Is there a Hashtable somewhere? Read the comments and also read the description and explanation that is given before and after the code on MSDN.
George2
May 14th, 2008, 12:22 AM
Thanks Shuja,
Some further questions after study.
1.
You mean CLR will create a new assembly (DLL/EXE) for each type I need to serialize, other than using existing CLR assembly (e.g. System.XML.dll)?
For example, when I want to serialize type Foo, CLR will create something like FooSerialize.dll?
2.
The additional dynamically created assembly will be deleted when I unload the Appdomain? And next time when I run my assembly again, each of the dynamically created assemblies will be created again?
3.
My assembly will invoke the dynamically created assemblies to do serialize/deserialize?
4.
I think the following line of code is not necessary, agree? :-)
XmlSerializer s = new XmlSerializer(typeof(MyClass), myRoot);
regards,
George
verifier
May 14th, 2008, 01:18 AM
2.
For the following sample, why creates two XMLSerializer instances, one is s and the other is ser?
That code is useless. It's made by someone who doesn't understand how the .net serializer works. They think that they need to keep a copy of the serializer in the memory, but that is done by the framework.
Arjay
May 14th, 2008, 02:32 AM
Thanks Shuja,
Some further questions after study.
1.
You mean CLR will create a new assembly (DLL/EXE) for each type I need to serialize, other than using existing CLR assembly (e.g. System.XML.dll)?
For example, when I want to serialize type Foo, CLR will create something like FooSerialize.dll?
2.
The additional dynamically created assembly will be deleted when I unload the Appdomain? And next time when I run my assembly again, each of the dynamically created assemblies will be created again?
3.
My assembly will invoke the dynamically created assemblies to do serialize/deserialize?
4.
I think the following line of code is not necessary, agree? :-)
XmlSerializer s = new XmlSerializer(typeof(MyClass), myRoot);
regards,
GeorgeWhat specific issues are you having? Is performance not hat you are expecting? What is the problem (other than theorectical)? As far as 4.... how else will the serializer for the object be defined? Have you tried coding it without? George please try these things BEFORE asking questions that you, yourself can answer.
George2
May 14th, 2008, 07:25 AM
Thanks verifier,
Sorry my English is not very good. Do you mean the code with ser or s is not correct?
That code is useless. It's made by someone who doesn't understand how the .net serializer works. They think that they need to keep a copy of the serializer in the memory, but that is done by the framework.
regards,
George
George2
May 14th, 2008, 07:27 AM
Thanks Arjay,
I already have some working code -- from functional perspective. But I want to learn more. From the MSDN page, I am confused about what means the additional assembly CLR will generate, which is something I do not know yet.
It contradicts with my previous understanding of how function call works, for example when we call function in System name space, we just call into System.dll, no need to generate any additional temporary DLL or something.
It is appreciated if you could clarify. :-)
What specific issues are you having? Is performance not hat you are expecting? What is the problem (other than theorectical)? As far as 4.... how else will the serializer for the object be defined? Have you tried coding it without? George please try these things BEFORE asking questions that you, yourself can answer.
regards,
George
verifier
May 14th, 2008, 07:47 AM
Thanks verifier,
Sorry my English is not very good. Do you mean the code with ser or s is not correct?
regards,
George
No problem. English is not my native language either.
The only thing you need is
XmlSerializer s = new XmlSerializer(typeof(MyClass), myRoot);
You can throw away the rest.
You asked why you must supply both the type and the object (since the type can be retrieved by using myRoot.GetType()).
I think that the purpose is if you want to serialize just a base class and not the instance class (for example you want xml for User and not employee if myRoot is "class Employee : User").
Shuja Ali
May 14th, 2008, 08:33 AM
I already have some working code -- from functional perspective. But I want to learn more. From the MSDN page, I am confused about what means the additional assembly CLR will generate, which is something I do not know yet.Serialization is something that works somewhat differently. When you serializae an object, the CLR builds a diferent assembly for that Class and serializes the object. This assembly is generated at run-time and subsequent serialization uses this dynamically generated assembly for the same classes. Basically when you use XMLSerializer, it actually generates some code that is compiled into an assembly dynamically and this assembly tranforms an Object into XML Stream.
It contradicts with my previous understanding of how function call works, for example when we call function in System name space, we just call into System.dll, no need to generate any additional temporary DLL or something.No it does not contradict with your previous understanding. Serialzing an object is different than doing a simple function call.
TheCPUWizard
May 14th, 2008, 08:39 AM
. When you serializae an object, the CLR builds a diferent assembly for that Class and serializes the object. This assembly is generated at run-time .
Unless you set your project properties to generate XMLSerializer classes at Build Time. ;) ;)
Shuja Ali
May 14th, 2008, 09:16 AM
Unless you set your project properties to generate XMLSerializer classes at Build Time. ;) ;)
You can also use SGEN.EXE tool. :)
Actually this was in response George's question ;)
George2
May 14th, 2008, 09:25 PM
Thanks Shuja Ali,
Your reply is very interesting. :-)
1.
The dynamically created assembly is another assembly other than my own assembly, which is dedicatedly working on Object to/from XML transformation?
2.
The additional dynamically created assembly will be deleted when I stop the process? And next time when I run my assembly again, each of the dynamically created assemblies will be created again?
3.
I read the MSDN sample from my original question post again, it seems that even if the process is running, when we create the same Serializer for the same type, if without optimization to keep the handle of the Serializer, the Serializer dynamically generated assembly will be created again?
(I mean they are never persistent to disk or something?)
Serialization is something that works somewhat differently. When you serializae an object, the CLR builds a diferent assembly for that Class and serializes the object. This assembly is generated at run-time and subsequent serialization uses this dynamically generated assembly for the same classes. Basically when you use XMLSerializer, it actually generates some code that is compiled into an assembly dynamically and this assembly tranforms an Object into XML Stream.
No it does not contradict with your previous understanding. Serialzing an object is different than doing a simple function call.
regards,
George
George2
May 14th, 2008, 09:28 PM
Thanks verifier,
After reading post #9 from Shuja Ali, I have some different options.
I think to make optimization, we should use the following code,
Hashtable serializers = new Hashtable();
// Implement a method named GenerateKey that creates unique keys
// for each instance of the XmlSerializer. The code should take
// into account all parameters passed to the XmlSerializer
// constructor.
object key = GenerateKey(typeof(MyClass), myRoot);
// Check the local cache for a matching serializer.
XmlSerializer ser = (XmlSerializer)serializers[key];
if (ser == null)
{
ser = new XmlSerializer(typeof(MyClass), myRoot);
// Cache the serializer.
serializers[key] = ser;
}
else
{
// Use the serializer to serialize, or deserialize.
}
and remove the following code from MSDN sample,
// Use the constructor that takes a type and XmlRootAttribute.
XmlSerializer s = new XmlSerializer(typeof(MyClass), myRoot);
Any comments?
No problem. English is not my native language either.
The only thing you need is
XmlSerializer s = new XmlSerializer(typeof(MyClass), myRoot);
You can throw away the rest.
You asked why you must supply both the type and the object (since the type can be retrieved by using myRoot.GetType()).
I think that the purpose is if you want to serialize just a base class and not the instance class (for example you want xml for User and not employee if myRoot is "class Employee : User").
regards,
George
Arjay
May 14th, 2008, 09:31 PM
Any comments?When you tested this, what performance differences did you see?
George2
May 14th, 2008, 09:58 PM
Thanks Arjay,
I only have two classes -- means 2 different types, there are not too many performance gains. But I have technical interests to learn something more, especially how to consider in the future when I have 200 classes. :-)
When you tested this, what performance differences did you see?
regards,
George
dglienna
May 14th, 2008, 10:41 PM
Why don't you try it, and then tell us? :) (might mean some extra work though)
George2
May 15th, 2008, 12:34 AM
Thanks dglienna,
I can try the performance, but not the questions in my post #12 about how internally CLR treats the dynamically created assemblies. :-)
Why don't you try it, and then tell us? :) (might mean some extra work though)
regards,
George
Arjay
May 15th, 2008, 12:38 AM
I only have two classes -- means 2 different types, there are not too many performance gains. But I have technical interests to learn something more, especially how to consider in the future when I have 200 classes. :-)You've heard of the concept of over optimization? It's where a developer attempts to optimize a bit of code before understanding whether or not it's a bottleneck. Here you have two options. Either build a serializer object on the fly or precompile the serializers you need as mentioned by TheCPUWizard and others. As far as caching a serializer that is built on the fly, we get back to over (or premature) optimization. But rather and ask this question, just test for it yourself and save forum bandwidth. You can then post your results and let us know what you find. Btw, while your tests are running, check out the forum and see if you can knock out any of the questions - that would be a real help if additional folks would contribute.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.