1

I have some base objects "Car", "dog", "cat" they implement an interface "IGWUIElement". I have a list of these interfaces: List myList.

At runtime I loop through the list of my elements and by examining the name of the class (using reflection) i need to populate their properties - which are not a part of the interface). I have an xml document descripting the propeties and the value i should assign to them. Here is my interface instantiation.

IGWUIElement newUIElement = (IGWUIElement)Activator.CreateInstance(result);

How do I go about invoking the properties from their name with a particular value (Please note datatypes are limited to int and string). Each object have different properties.

Hope this makes sense...

/H4mm3r

2 Answers 2

5

Use PropertyInfo.SetValue()

PropertyInfo piInstance = typeof(IGWUIElement).GetProperty("property_name");
piInstance.SetValue(newUIElement, value, null);

More on msdn.

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

Comments

4

You can do this like so:

IGWUIElement element = myList[0];

// Set a string property
element.GetType().InvokeMember("SomeStringProperty", BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty, Type.DefaultBinder, element, "The String Value");

// Set an int property
element.GetType().InvokeMember("SomeIntProperty", BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty, Type.DefaultBinder, element, 3);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.