A sequence of properties in a C # class

We have a file format that we need to analyze, which looks like this:

v1|000|sammy|endpoint|blah 

This is an ordered fixed-width format provided by the supplier, so each of these 5 fields is mapped to a specific property in the class (the actual format has> 30).

I would simply analyze this with Reflection, applying a sequence to the properties. One way to do this is to simply create something yourself - write an attribute class that takes one number, and apply this attribute to each property with its sequence index and look for it during Reflection in the OrderBy clause.

Is there an existing or better way to do this in C #? For example, is there an attribute for this already? Is there any way to ask in C #, or maybe even MSIL, what order properties were declared in the class?

+4
source share
7 answers

The order in which properties are displayed in the metadata is displayed using PropertyInfo.MetadataToken . It so happened that the current compiler will make this order coincide with the order in which the properties appear in the source code, therefore, ordering the MetadataToken , you get the same order as in the source code.

Disclaimer: A future compiler may change this. This will probably not happen if there is no reason for this, but if the compiler, for example, becomes multithreaded, additional unnecessary efforts may be required to maintain the original order. If you rely on this, make sure you get a hard error, not a quiet loss of runtime if / when the .NET Framework is updated so that it breaks.

+4
source

I would personally make a special attribute for this if you want to use an attribute based approach. This is not a “standard” operation, so there is no (suitable) attribute in the structure that can be used to decorate your classes.

My approach is likely to be a class level attribute that took an array of strings for property names for each entry in the list or something like these strings.

In doing so, I wonder if the attribute-based approach is the right approach at all. You will probably need some type of manager that mediates this, since something will need to be done for the “reflection” process. It might be wiser for this class to manage relationships here, especially since it already needs to know your class hierarchy (in order to create the class first).

At that moment, when a custom class or method that can directly construct an object will work better, be more convenient for maintenance, and be much simpler than trying to use reflection and doing it dynamically.

+1
source

Are you using .net 4.0? It looks like a dynamic keyword was created. Namely, it seems that order and consistency are more important than specific types that can be at any given time, so you can just arbitrarily assign headers, data, whatever for a dynamic object, according to the rules that you like, and then pull them back using the same rules. It also (presumably) allows you not to use reflection, which is always a plus.

+1
source

I would recommend parsing using something like FileHelpers .

+1
source

Now, if performance is not a big problem, and you go with Reflection, then an easy way to get a mapping without attributes is to analyze the use of groups using RegEx. Similar to this implementation: Read a fixed width record from a text file

Uses a regular expression, for example:

 "^(?<Field1>.{6})(?<Field2>.{16})(?<Field3>.{12})" 

Since you can define group names yourself, you could reasonably choose names that will exactly match your property names, and thus automatically map Reflection without using attributes.

EDIT: Given that you get property names inside a string that will not be very “convenient for refactoring,” I would strongly recommend unit testing this completely to ensure that renaming your properties will break the test if a mismatch occurs.

+1
source

You can look at an implementation of something similar to Google Protocol Buffers .

There is currently no C # implementation (which I know), but the documentation provided is very good and should give you some ideas that will work better than reflection, which is much slower and usually complicated.

0
source

Here, of course, there are many possible answers, so this is how I met:

There is an existing attribute in System.ComponentModel.DataAnnotations (in .Net 4.5+, it is moved to System.ComponentModel.DataAnnotations.Schema) with the name ColumnAttribute:

http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.schema.columnattribute(v=vs.110 )

You can use it like:

 [Column(Order=1)] public string Version { get; set; } [Column(Order=2)] public string Id { get; set; } 

But this is clearly annoying the update, if the fixed-width format changes - you need to manually enter and change the 30+ ordinals that you entered, if you say, the field is added to the beginning. Since we do not control the format in this scenario, and future versions may occur frequently, it would be nice to find something with an implied sequence of order properties entered into the class.

0
source

All Articles