53

Possible Duplicate:
.Net - Reflection set object property
Setting a property by reflection with a string value

I have an object with multiple properties. Let's call the object objName. I'm trying to create a method that simply updates the object with the new property values.

I want to be able to do the following in a method:

private void SetObjectProperty(string propertyName, string value, ref object objName)
{
    //some processing on the rest of the code to make sure we actually want to set this value.
    objName.propertyName = value
}

and finally, the call:

SetObjectProperty("nameOfProperty", textBoxValue.Text, ref objName);

Hope the question is fleshed out enough. Let me know if you need more details.

Thanks for the answers all!

2
  • @DavidArcher there is no rel keyboard in C#...I take it you mean ref? There is no need to pass an object as ref unless you intend on changing the actual instance of it. Commented Oct 19, 2012 at 8:55
  • Indeed, I did mean ref, and yes, I do intend on changing the actual instance. Commented Oct 19, 2012 at 9:00

5 Answers 5

90

objName.GetType().GetProperty("nameOfProperty").SetValue(objName, objValue, null)

Sign up to request clarification or add additional context in comments.

4 Comments

You should use propertyName inside GetProperty().
And what if "nameOfProperty" doesn't exist?
Exception of course, you can use GetProperties to test.
I was able to replace my 200 line code with 5 lines using your code sample. Thanks, you saved my day.
47

You can use Reflection to do this e.g.

private void SetObjectProperty(string propertyName, string value, object obj)
{
    PropertyInfo propertyInfo = obj.GetType().GetProperty(propertyName);
    // make sure object has the property we are after
    if (propertyInfo != null)
    {
        propertyInfo.SetValue(obj, value, null);
    }
}

3 Comments

Props for checking for null before calling.
I usually also check for "can write": if (propertyInfo != null && propertyInfo.CanWrite).
How if I want to set static property Sir?
4

You can use Type.InvokeMember to do this.

private void SetObjectProperty(string propertyName, string value, rel objName) 
{ 
    objName.GetType().InvokeMember(propertyName, 
        BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty, 
        Type.DefaultBinder, objName, value); 
} 

Comments

4

Get the property info first, and then set the value on the property:

PropertyInfo propertyInfo = objName.GetType().GetProperty(propertyName);
propertyInfo.SetValue(objName, value, null);

Comments

2

You can do it via reflection:

void SetObjectProperty(object theObject, string propertyName, object value)
{
  Type type=theObject.GetType();
  var property=type.GetProperty(propertyName);
  var setter=property.SetMethod();
  setter.Invoke(theObject, new ojbject[]{value});
}

NOTE: Error handling intentionally left out for the sake of readability.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.