473,382 Members | 1,749 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Assign Property value using Reflection

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,

--

Andrew Robinson
Jul 21 '06 #1
6 2773
Check out the System.Reflection.PropertyInfo object.

Jul 21 '06 #2
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);

}
--

Andrew Robinson
"allfyre" <al*****@hotmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
Check out the System.Reflection.PropertyInfo object.

Jul 21 '06 #3
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/subscripti...ult.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/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 24 '06 #4
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

""Jeffrey Tan[MSFT]"" wrote:
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/subscripti...ult.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/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Sep 5 '06 #5
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
Sep 5 '06 #6
Marc,

Thanks very much

I'll try it out.

Regards
Jeremy
"Marc Gravell" wrote:
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
Sep 7 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Eric | last post by:
How can I dynamically assign an event to an element? I have tried : (myelement is a text input) document.getElementById('myelement').onKeyUp = "myfnc(param1,param2,param3)"; ...
5
by: Nicolas | last post by:
How can I simply do this. string tAlign; tAlign="Left"; TextBox textbox1 = new TextBox(); textbox1.TextAlign = tAlign; I know that it shoul be like "textbox1.TextAlign =
1
by: Mudassar | last post by:
i want to get the property value using reflection. Scenerio: i have a status bar on MDI form. it has property named "Panels" and i want to get a specific panel from that panels collection using...
8
by: critter | last post by:
I am trying to get a value of a property of a class, but not an instance of the class. Using Reflection, I can find the class in my assembly, and I can find the property of my class, however, I...
11
by: Aaron Queenan | last post by:
Given the classes: class Class { public static implicit operator int(Class c) { return 0; } } class Holder
2
by: André | last post by:
I try to initialize some Public Variable in a class use for code-behind The idea is to check all uxMsgXXX variables, declare in top of my class and assign the string value from a DB request. Why...
4
by: Pritcham | last post by:
Hi all I've got a number of classes already developed (basic entity classes) like the following: Public Class Contact Private _firstname as String Private _age as Integer Public Property...
4
by: Steve Goldman | last post by:
Even asking this question probably demonstrates that I have a fundamental misunderstanding of how values and references work in C#, but here goes: I'd like to assign a reference to an arbitrary...
2
balabaster
by: balabaster | last post by:
okay, I'm trying to reference a property within a class using a string "class.property", I've got a test class, imaginitively called "Test", hold your applause...it's got a property called value. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.