posts - 234, comments - 480, trackbacks - 56

My Links

News




I am born in Bangladesh and currently live in Melbourne, Australia. I am a co-founder and core developer of Pageflakes www.pageflakes.com and CEO at Simplexhub, a highly experienced software development company based in Melbourne Australia and Dhaka, Bangladesh.

I also created SmartCodeGenerator

Some of 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
Gravatar

# Object does not match target type ...

Hi Shahed ,

I am having one text box class with properties
Dim tValue As Object = Nothing
Dim obj As Object = GetType(cstmTextBox)
Dim piInfo As PropertyInfo = obj.GetProperty("DataType")
tValue = piInfo.GetValue(obj, Nothing)

im getting this error Object does not match target type ...

what to do ?
6/26/2009 3:41 PM | Raj
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: