doesn't actually set the value...

I was given the following code to set a value in an object from a string like "Header.field.str".  It seems to function correctly but never sets the value.  Any help is much appreciated...


public static void SetValue(object baseObj, string fieldName, object value)

{

MemberInfo member = null; //The current field/property matched

Type typeCurr = baseObj.GetType(); //The current type of the field/property matched

object data = baseObj; //The previous object/property that matched

string[] names = fieldName.Split('.');

foreach (string name in names)

{

//We are still going so get the current data object that we might

//need to set

if (member is PropertyInfo)

data = ((PropertyInfo)member).GetValue(data, null);

else if (member is FieldInfo)

data = ((FieldInfo)member).GetValue(data);

//Find the next property/field

PropertyInfo prop = typeCurr.GetProperty(name);

if (prop != null)

{

typeCurr = prop.PropertyType;

member = prop;

}

else //Not a property so try the fields

{

FieldInfo field = typeCurr.GetField(name);

if (field != null)

{

typeCurr = field.FieldType;

member = field;

};

};

};

// if (member is FieldInfo)

// data = ((FieldInfo)member).GetValue(data);

if (member is PropertyInfo)

{

((PropertyInfo)member).SetValue(data, value, null);

}

else

{

//((FieldInfo)member).SetValue(data, value);

((FieldInfo)member).SetValue(data, value);

};

}


 



Answer this question

doesn't actually set the value...

  • hrh

    I might be missing something but I think all you need to do is this:


    baseObj.GetType().InvokeMember(name, 
                BindingFlags.Public | BindingFlags.NonPublic | 
                BindingFlags.SetField | BindingFlags.Static,
                null, baseobj, new object[] {value});

     



  • ScottLeo

    you mean like a nested class property -


    public class AClass
    {
       public Nested
       {
          int nestedInt
       }
       int baseInt;
    }

     


    sounds like a need for recursion on the same method - do you need me to elaborate more

  • RedHedToo

    Yes please, I've tried recursion using type and fieldinfo and getvalue, but what I've found is that getvalue doesn't return a reference to the actuall object but rather a new object.  So when I set the value of object, it doesn't set the original object's value.

    Thanks,

    Devin

  • cllard

    Thanks,  It works if your setting a value that's in the objects member scope, but if it's nested within on of the members then it fails.
  • Adam Friedman

    OK perhaps a better question is to ask, does anybody know how to set an objects value using reflection
  • Jakin

    OK, I finally figured it out...



    public static object GetValue(object obj, string path)

    {

    return TraverseObjectValue(obj, path.Split('.'), null);

    }

    public static void SetValue(object obj, string path, object value)

    {

    object o = TraverseObjectValue(obj, path.Split('.'), value);

    }

    public static object TraverseObjectValue(object lastobj, string[] paths, object value)

    {

    Type t = lastobj.GetType();

    FieldInfo f = t.GetField(paths[0]);

    object o = f.GetValue(lastobj);

    if (paths.GetLength(0) > 1)//if not at end value

    {

    string[] p = new string[paths.GetUpperBound(0)];

    for (int i = 1; i < paths.GetLength(0); i++)

    p[i - 1] = pathsIdea;

    object oo = TraverseObjectValue(o, p, value);

    if (value != null)

    f.SetValue(lastobj, o);

    return oo;

    }

    else//if at end value

    {

    if (value != null)

    f.SetValue(lastobj, value);

    return o;

    }

    }


     


    I figured out that obtaining the object from the field was creating a new object instead of a reference.  You'll see that I assigned the newly created object to the parent object to allow for the recursion.

  • doesn't actually set the value...