Home > Enterprise >  Convert strings to data object name and fields
Convert strings to data object name and fields

Time:06-18

I’m going to get a list from a database consisting of information to pull data from a delimited file and load it into a number of data objects. It'll include the table the data is going into, the data row to look in within the a data file, and the spot in the row to grab.

Right now it’s hard coded as so - TransLevel2.CD_FIELD_1 being a field in a data object on my side:

case "SAC":
                    TransLevel2 = new TransLevel2();

                    TransLevel2.CD_FIELD_1 = TryGetString(transLine, 1);

(TryGetString being a function to get value x from the row in question from the field, included at request)

    public static string TryGetString(string[] transLine, int position)
    {
        return position > transLine.Length - 1 ? string.Empty : transLine[position];
    }

So this list would give me TransLevel2, CD_FIELD_1, SAC and 1.

Using the field number is easy:

                    int testint=1
                    TransLevel2.CD_FIELD_1 = TryGetString(transLine, testint);

The question is, how can I convert the strings TransLevel2 and CD_FIELD_1 into the reference to then load the object?

ADDENDUM - Trying to make myself more clear -

Here's the class I'm trying to assign a value to:

public class TransLevel2
{
    public string CD_FIELD_1 { get; set; }
}

And the DB call gives me two strings containing "TransLevel2" and "CD_FIELD_1". Those two strings tell me what object (there's a few) and field I need to assign the value to.

So the issue is trying to take those two strings and convert them so I can make the assignment

 TransLevel2.CD_FIELD_1 = "tree";

So this is effectively what I'm trying to to, but it shouldn't possibly be the only way to do it. For two input strings, and assuming the classes referenced are created:

            switch (strobject)
            {
                case "Object1":
                    switch (strfield)
                    {
                        case "field1":
                            Object1.field1 = "tree";
                            break;

                        case "field2":
                            Object1.field2 = "tree";
                            break;
                    }


                    break;

                case "Object2":
                    switch (strfield)
                    {
                        case "field1":
                            Object2.field1 = "tree";
                            break;

                        case "field2":
                            Object2.field2 = "tree";
                            break;
                    }

                    break;


            }

CodePudding user response:

So, if I understood your question correctly, you have three strings:

  • strobject
  • strfield
  • and probably also strvalue (hardcoded as "tree" in your example),

and you want to

  1. create a new object of a type whose name is contained in strobject and
  2. set the property whose name is contained in strfield to the value contained in strvalue.

This can be done with reflection. First, we create the new object instance:

object o = Activator.CreateInstance(null, "Namespace.Where.Your.Classes.Are."   strobject);

then we get the property we need:

var propertyInfo = o.GetType().GetProperty(strfield);

and the we assign the value:

propertyInfo.SetValue(o, strvalue, null);

(Note: It sounds like you are building a simple ORM tool. That totally works, and I've done that myself, but if you are just starting, you might want to consider using an existing product instead.)

CodePudding user response:

We have a bingo - Heinzi got me where I needed to go:

        ObjectHandle O_810 = Activator.CreateInstance(null, "810");
        Edi810 l_810 = (810)O_810.Unwrap();

        var propertyInfo = l_810.GetType().GetProperty(str);
        propertyInfo.SetValue(l_810, "tree"), null);

His code didn't let me get at the types, I apparently needed to unwrap it first.

  • Related