How to set default values to the properties of dynamically loaded types at runtime for XML serializa

Posted by Erkan Y. on Stack Overflow See other posts from Stack Overflow or by Erkan Y.
Published on 2010-06-01T18:20:10Z Indexed on 2010/06/01 18:23 UTC
Read the original article Hit count: 247

Filed under:
|
|
|

I need to serialize dynamically loaded types' classes using XMLSerializer.

When using XML serializer non initialized values are not being serialized. I dont have control over the assemblies I am working with so can not use XML attributes for specifying default values on properties. So I think I need to set all properties and sub properties to their default values recursively and then serialize. ( Please let me know if there is any better way )

Followed this :

Activator.CreateInstance(propType);

but above line complains about not having a parameterless constructor for some types.

Tried this : subObject = FormatterServices.GetUninitializedObject(propType); but this one gives an error "value was invalid" with no inner exception.

Please let me know if you need any further information.

© Stack Overflow or respective owner

Related posts about c#

Related posts about reflection