posts - 218, comments - 222, trackbacks - 68

My Links

News




I am a Microsoft Certified Application Developer MCAD Chartered Member (C# .Net) and born in Bangladesh.
I work for Ocean Informatics Pty Ltd as a Senior Developer - Analyst.
I am also co-founder and core developer of Pageflakes (acquired by LiveUniverse) www.pageflakes.com
and most recently created SmartCodeGenerator

My Articles
Flexible and Plugin based .Net Application..
Mass Emailing Functionality with C#, .NET 2.0, and Microsoft® SQL Server 2005 Service Broker'
Write your own Code Generator or Template Engine in .NET
Smart Code Generator .NET: Usage Overview
Smart Code Generator .NET: Architectural Overview
Smart Code Generator .NET: using with NAnt and Cassini

Archives

Free Programming Language Training

Using Reflection to Get and Set values of Properties

The assembly that is generated for .Net Framework has metadata that describes the structure of the assembly and its classes. By using reflection we can investigate the structure of classes and the data that assembly holds dynamically at runtime. Couple of days back in this project that I am working I had to dynamically get/set  values from/to properties of object. The following code will give you a flavor of what I did.

To Get  value
===============

foreach (PropertyInfo info in myObject.GetType().GetProperties())
{
   if (info.CanRead)
   {
      object o = propertyInfo.GetValue(myObject, null);
   }
}

To Set  value
================

object myValue = "Something";
if (propertyInfo.CanWrite)
{
    this.propertyInfo.SetValue(myObject, myValue, null);
}

Print | posted on Sunday, November 19, 2006 12:29 PM |

Feedback

Gravatar

# re: Using Reflection to Get and Set values of Properties

I have a problem.

PropertyInfo inf = typeof(TextBox).GetProperty("Font");
PropertyInfo inFont = inf.PropertyType.GetProperty("Bold");
inFont.SetValue(ctrl, true, null);

ctrl's type is TextBox.
But I've an error.

"Object does not match target type"

Why?
2/20/2007 3:54 PM | filiz
Gravatar

# re: Using Reflection to Get and Set values of Properties

The first line of the code...
PropertyInfo inf = typeof(TextBox).GetProperty("Font");

you need an instance of an object and then should typecast.

Say TextBox textBox1 = new Textbox;
PropertyInfo inf = textBox1.GetType().GetProperty("Font");

Try like this and let me know....
2/21/2007 2:45 AM | Shahed Khan
Gravatar

# re: Using Reflection to Get and Set values of Properties

For the "Bold" Property you will see
CanWrite false bool

It allows only get...
So it does not allow you to setvalue.

Hope this helps!

2/21/2007 3:03 AM | Shahed Khan
Gravatar

# re: Using Reflection to Get and Set values of Properties

Here is a way to change the Fonts FontStyle...

Font font = new Font(textBox1.Font, FontStyle.Bold);
textBox1.Font = font;
2/21/2007 3:10 AM | Shahed Khan
Gravatar

# re: Using Reflection to Get and Set values of Properties

Font property's CanWrite = false
but
Bold property's CanWrite = true

PropertyInfo inf = ctrl.GetType().GetProperty("Font");
PropertyInfo inFont = inf.PropertyType.GetProperty("Bold");
inFont.SetValue(ctrl, true, null);

Actually I want to access for a webcontrol font property's bold property.

How can I do?
2/21/2007 7:53 AM | filiz
Gravatar

# re: Using Reflection to Get and Set values of Properties

Very Good example.
9/20/2007 9:05 AM | Sunil
Gravatar

# re: Using Reflection to Get and Set values of Properties

You need to get the value of inf first, which is the Font object itself. You don't need the inFont PropertyInfo object.

Font controlFont = (Font)inf.GetValue(ctrl, null);

then...

controlFont.Bold = true;

then...

inf.SetValue(ctrl, controlFont, null);

Good luck!

Ricardo.
9/8/2008 7:41 AM | Ricardo Villamil

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 7 and 1 and type the answer here:

Powered by: