Assign Property value using Reflection

A

Andrew Robinson

assuming that I have a class

public class MyClass {
public string FirstName {
get {...}
set {...}
}
}

how can I assign the FirstName property using reflection on a generic class
of type MyClass?

string propertyName = "FirstName";
C is a gerneric type of type "MyClass"

so what I want to do is:
C.propertyName = "Andrew"; // <-pseudo code...

For the simplicity, lets assume that the property is always of type string
but it could very (an I will know the type ahead of time.)

Thanks,
 
A

Andrew Robinson

Thanks for the push in the right direction:

D d = new D();



foreach (Map map in mapList)

{

PropertyInfo property = typeof(D).GetProperty(map.PropertyName);



if (!property.CanWrite)

{

string message = "Unable to set property {0} of type {1}";

throw new InvalidOperationException(string.Format(message,
map.PropertyName, typeof(D).Name));

}



property.SetValue(d, dr[map.ColumnName], null);

}
 
J

Jeffrey Tan[MSFT]

Hi Andrew ,

I am glad you found the solution yourself. Actually, all the Reflection
related classes are stored in System.Reflection namespace, so you may check
the class names under this namespace for what you want. Normally, you may
search the class name with "property" included. :)

If you need further help, please feel free to post. Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Jeffrey,

I wonder if you could help me with something similar (I've not managed to
find the info in the online dox!).

I need to add to a collection using reflection, specifically
combo.Items.Add(myValue) where combo could be a DropDownList,
ToolStripComboBox, ComboBox, DataGridViewComboBox etc.

I've got as far as :

Dim prp as PropertyInfo = combo.GetProperty("Items")
For Each s as String in strings
--> pseudo code <-- prp.Add(s)
Next

Many thanks
 
M

Marc Gravell

Most (if not all) of these .Items properties will implement IList; hence,
the easiest option is:

IList items = (IList) obj.GetType().GetProperty("Items").GetValue(obj,
null);
foreach(string s in strings) {
items.Add(s);
}

Note that the Items property is rarely settable, but that
PropertyInfo.SetValue does this job when it can...

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top