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. Simplexhub, is on its mission to build a smart virtual community in Bangladesh and recently launched beta realestatebazaar.com.bd an ASP.NET MVC application written in C#.NET.

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

Set Enum Value with reflection .Net1.1 and .Net2.0

While trying to set value for enum via Reflection I discovered .Net2.0 is much smarter in setting values.

In .Net2.0 the following works:

Int32 property = int.Parse("20");
if (propertyInfo.CanWrite)
{
  this.propertyInfo.SetValue(ParentPage.TheProperties, property, null);
}

but this same code as above will not work in .Net1.1 and will throw a cast exception. Here is the workaround for .Net1.1, where we explicitly do Enum.ToObject(...) . ie.

object property = int.Parse("20");
object newEnumValue = Enum.ToObject(propertyInfo.PropertyType, property);

if (propertyInfo.CanWrite)
{
  this.propertyInfo.SetValue(ParentPage.TheProperties, newEnumValue, null);
}

Print | posted on Wednesday, December 06, 2006 2:51 AM |

Feedback

No comments posted yet.
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: