How to save a link to an object property in another object?

This is a winforms c # application.

Introduction:

I am creating a form that will allow users to import data from an existing MySql or SQL Server database into my SQL Server database. This allows users to quickly import IP addresses and other data without re-entering it through the control.

Example:

Simplified, I have two objects, FieldObject and SensorObject . FieldObject exists to store the names and types of fields in the source and destination database. SensorObject is an object that I later populate from records in the database. For simplicity, I omit type handling and other functions that are not relevant to the question. (The data for the DestinationField limited to the list that I provide to the user, and comes from an array or list inside the program.) Here is an example of both:

 public class FieldObject { public string DestinationField {get; set;} public string SourceField {get; set;} } public class SensorObject { public string Name {get; set;} public string IPAddress {get; set;} } 

Problem:

When a user fills in various FieldObject fields, I use this information to populate the FieldObject database, although I have a large switch statement that checks the name of the SensorObject field to find out which SensorObject property it belongs to.

For example:

 // Reader is a SqlDataReader with the prerequisite database connection FieldObject myField = new FieldObject { DestinationField = "name", SourceField = "proprietary" }; SensorObject mySensor = new SensorObject(); switch (myField.DestinationField) { case "name": mySensor.Name = Convert.ToString(Reader[myField.DestinationField]); break; case "ip_address": mySensor.IPAddress = Convert.ToString(Reader[myField.DestinationField]); break; } 

As you can see, processing more properties for an object will require more redundant code.

Question:

I would like to somehow preserve the SensorObject property to which the data belongs, so when repeating FieldObjects in the list, I can effectively exclude the switch statement.

Something like:

 foreach(FieldObject f in myFieldList) { mySensor(f.mySensorField) = Convert.ToString(Reader[f.DestinationField]); } 

I'm not sure what technique in C # lends itself to this kind of application. I looked at reference values ​​and reflection, and they do not seem to be suitable.

I appreciate understanding, advice, and even ways to rethink this approach.

+6
object c # properties winforms
source share
3 answers

You must use reflection to do this. It should end up something like:

 var sensorFields = typeof(SensorObject).GetProperties() foreach(var field in fields) { var info = sensorFields.First(sensorField => sensorField.Name == field.Name); var value = Convert.ToString(Reader[field.Destination]); info.SetValue(sensorObj, value, new object[0]); } 

GetProperties gets property information for each property that can be used to set the value.

Of course, property information can be cached. Just write it once and refactor as soon as it starts. Do not complicate too much, it is called premature optimization and leads directly to hell;)

+2
source share

Actually, reflection is a good idea, especially if you populate the dictionary with PropertyInfo instances for each property name.

+1
source share

You can do it with reflection. You get the PropertyInfo properties with the name in question, get the MethodInfo installer, and then call it.

It may be possible to store an associative array (dictionary or HastTable, etc.) of values ​​stored by name, which would be much easier if necessary.

+1
source share

All Articles