PC Review


Reply
Thread Tools Rate Thread

Assign Property value using Reflection

 
 
Andrew Robinson
Guest
Posts: n/a
 
      21st Jul 2006
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


 
Reply With Quote
 
 
 
 
allfyre
Guest
Posts: n/a
 
      21st Jul 2006
Check out the System.Reflection.PropertyInfo object.

 
Reply With Quote
 
Andrew Robinson
Guest
Posts: n/a
 
      21st Jul 2006
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Check out the System.Reflection.PropertyInfo object.
>



 
Reply With Quote
 
Jeffrey Tan[MSFT]
Guest
Posts: n/a
 
      24th Jul 2006
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.

 
Reply With Quote
 
=?Utf-8?B?SmVyZW15SG9sdA==?=
Guest
Posts: n/a
 
      5th Sep 2006
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.
>
>

 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      5th Sep 2006
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


 
Reply With Quote
 
=?Utf-8?B?SmVyZW15SG9sdA==?=
Guest
Posts: n/a
 
      7th Sep 2006
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
>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Assign a value to variable via Reflection where var name and valueare pulled from db Johhny Marr Microsoft C# .NET 0 31st Oct 2010 10:47 AM
Assign property from db to object property? Burt Microsoft VB .NET 2 7th Feb 2006 10:33 PM
Assign all members this.Something with reflection? =?Utf-8?B?TVNETkFuZGk=?= Microsoft Dot NET Framework 9 16th Dec 2005 10:25 AM
Assign Public or Private variable thru Reflection vb.net/asp.net =?Utf-8?B?QW5kcsOp?= Microsoft VB .NET 2 13th Oct 2005 05:22 PM
Problem using Reflection to Assign Generic Delegates Joanna Carter \(TeamB\) Microsoft C# .NET 6 17th Feb 2005 09:57 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:51 PM.