Disable Anonymous Type

Possible duplicate:
How to XML deserialize an object of Unknown type?

I just started playing with serialization and deserialization.

I have a model of type Person, with two properties (Name and Age), I can serialize in XML. When it comes to deserialization, as far as I know, I have to tell the compiler that this is a type of Person. With the generic T world, this seems counterproductive. I would think that I can set the object as an InMemoryOnly object (if you imagine it), where the object is created at runtime, like the / fields / properties parameters, etc., They are all based on XML. Perhaps as an anonymous type; I understand that the C # compiler will have to ignore this at design time and that we will lose intellisense, but since all objects are passed into memory in some way, I cannot understand why this will happen at runtime. Or maybe it would be possible with Reflection?

In any case, this is what I am trying to do, deserializing from XML to a shared object. Is this possible with C #?

EDIT Just found a hoax, so now the question is why C # (.NET) doesn't do this? I know that the answer is probably "because it is not," but my question MAY be possible or there will be more problems than it costs for a programmer?

The reason for this is, as far as I know, to achieve this means that serialization and de-serialization must know the type (in this case, Person). So, if serailise and deserialize happen through WCF (where serialization and serialization happen in different assemblies), the Person object should be duplicated (and this contradicts the DRY principle).

+4
source share
1 answer

The WCF sample does not show a valid reason for this.
You just could - and you can! - put the Person class in the assembly, which is used by both the server and the client. This is how I do it when using WCF.
If you do not want to do this, Visual Studio offers to automatically generate the types used in the service when you add a link to the service.
In both cases, you only need to update the Person class in one place.

+3
source

Source: https://habr.com/ru/post/1439320/


All Articles