Getting LINQ vs Reflection Data

I was hoping someone would tell me which is a more efficient and / or correct way to get some data.

I have some XML files coming from a third party and their attached DTDs. So I converted the DTD to a C # class so that I could deserialize XML into classes. Now I need to map this data according to the settings of my data structures.

The question is ultimately; should use reflection or LINQ. The XML format is somewhat generalized in design, where things are stored in Items [Array] or Item [Object].

I have done the following:

TheirClass class = theirMessage.Items.Where(n=> n.GetType() == typeof(TheirClass)).First() as TheirClass; MyObject.Param1 = ConversionHelperClass.Convert(class.Obj1); MyObject.Param2 = ConversionHelperClass.Convert(class.Obj2); 

I can also do some things with Reflection, where I pass the names of the classes and attributes that I'm trying to hook.

Trying to do it right here.

+4
source share
1 answer

As a rule, I suggest avoiding reflection if absolutely necessary! It enters overhead overhead and means that you will miss all the wonderful compile-time checks that the compiler team worked so hard to give us.

Linq for entities essentially queries a dataset in memory, so it can be very fast.

If your ultimate goal is to parse the information from an XML document, I would suggest checking out the XDocument class. This provides a very good abstraction for querying XML documents.

+5
source

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


All Articles