Click here to Skip to main content
Click here to Skip to main content

Use .NET Reflection APIs to facilitate get/set property

By , 13 Jan 2005
 

Introduction

This is a utilities component for dynamic access to .NET objects' properties/fields. It offers low-level utility classes that assist in getting and setting property values on .NET classes.

In general, we design a data access tier for software system, then need dynamic get/set for the object properties. Of course, we must use .NET Reflection API, but we need a simple method.

For these purposes, I designed this utilities component.

Five formats for referencing a particular property value of an object are supported:

  • Simple (name) - The specified name identifies an individual property of a particular object.
  • Nested (name1.name2.name3) The first name element is used to select a property getter, as for simple references above. The object returned for this property is then consulted, using the same approach, for a property getter for a property named name2, and so on. The property value that is ultimately retrieved or modified is the one identified by the last name element.
  • Indexed (name[index]) - The underlying property value is assumed to be an array, or an instance of IList. The appropriate (zero-relative) entry in the array is selected.
  • Mapped (name[key]) - the name is an instance of IDictionary, key type is System.String.
  • Combined (name1.name2[index].name3[key]) - Combining mapped, nested, and indexed references is also supported.

Using the code

Simple Test:

[Test]
public void TestReadProperty()
{
    EmployeeInfo emp= GetNewEmployee();

    Assert.AreEqual("ID101",DynaAccessUtils.GetProperty(emp,"Emp_id"));
    Assert.AreEqual("cao",DynaAccessUtils.GetProperty(emp,"Fname"));
    Assert.AreEqual(new DateTime(2004,12,30), 
                    DynaAccessUtils.GetProperty(emp,"Hire_date"));
    Assert.AreEqual(200,DynaAccessUtils.GetProperty(emp,"Job_id"));
    Assert.AreEqual(10,DynaAccessUtils.GetProperty(emp,"Job_lvl"));
    Assert.AreEqual("ju",DynaAccessUtils.GetProperty(emp,"Lname"));
    Assert.AreEqual("Minit",DynaAccessUtils.GetProperty(emp,"Minit"));
    Assert.AreEqual("pubid",DynaAccessUtils.GetProperty(emp,"Pub_id"));
    Assert.AreEqual(  new Guid("00000000-0000-0000-0000-000000000000"), 
                                DynaAccessUtils.GetProperty(emp,"Id"));
}

[Test]
public void TestWriteProperty()
{
    EmployeeInfo emp= GetNewEmployee();

    DynaAccessUtils.SetProperty(emp,"Emp_id","ID202");
    DynaAccessUtils.SetProperty(emp,"Hire_date",new DateTime(2005,1,1));
    DynaAccessUtils.SetProperty(emp,"Job_id",300);

    Assert.AreEqual( "ID202",emp.Emp_id );
    Assert.AreEqual(new DateTime(2005,1,1) , emp.Hire_date);
    Assert.AreEqual(  300,emp.Job_id);

}

[Test]
public void TestWriteProperty2()
{
    EmployeeInfo emp= GetNewEmployee();

    DynaAccessUtils.SetProperty(emp,"Emp_id","ID202");
    DynaAccessUtils.SetProperty(emp,"Hire_date","2005-1-1");
    DynaAccessUtils.SetProperty(emp,"Job_id","300");
    DynaAccessUtils.SetProperty(emp,"Id","382c74c3-721d-4f34-80e5-57657b6cbc27");

    Assert.AreEqual( "ID202",emp.Emp_id );
    Assert.AreEqual(new DateTime(2005,1,1) , emp.Hire_date);
    Assert.AreEqual(  300,emp.Job_id);
    Assert.AreEqual(  new Guid("382c74c3-721d-4f34-80e5-57657b6cbc27"),emp.Id);

}

List Test:

[Test]
public void TestReadProperty()
{
    ArrayList list = new ArrayList();
    list.Add(GetNewEmployee("id001"));
    list.Add(GetNewEmployee("id002"));
    list.Add(GetNewEmployee("id003"));

    Assert.AreEqual("id001",DynaAccessUtils.GetProperty(list,"[0].Emp_id"));
    Assert.AreEqual("id002",DynaAccessUtils.GetProperty(list,"[1].Emp_id"));
    Assert.AreEqual("id003",DynaAccessUtils.GetProperty(list,"[2].Emp_id"));
}

[Test]
public void TestWriteProperty()
{
    ArrayList list = new ArrayList();
    list.Add(GetNewEmployee("id001"));
    list.Add(GetNewEmployee("id002"));
    list.Add(GetNewEmployee("id003"));

    DynaAccessUtils.SetProperty(list,"[0].Emp_id","new001");
    DynaAccessUtils.SetProperty(list,"[1].Emp_id","new002");
    DynaAccessUtils.SetProperty(list,"[2].Emp_id","new003");

    Assert.AreEqual("new001",((EmployeeInfo)list[0]).Emp_id);
    Assert.AreEqual("new002",((EmployeeInfo)list[1]).Emp_id);
    Assert.AreEqual("new003",((EmployeeInfo)list[2]).Emp_id);
}

Map Test:

[Test]
public void TestReadProperty()
{
    Hashtable map = new Hashtable();
    map.Add("first",GetNewEmployee("id001"));
    map.Add("second",GetNewEmployee("id002"));
    map.Add("three",GetNewEmployee("id003"));

    Assert.AreEqual("id001",DynaAccessUtils.GetProperty(map,"first.Emp_id"));
    Assert.AreEqual("id002",DynaAccessUtils.GetProperty(map,"second.Emp_id"));
    Assert.AreEqual("id003",DynaAccessUtils.GetProperty(map,"three.Emp_id"));
}

[Test]
public void TestWriteProperty()
{
    Hashtable map = new Hashtable();
    map.Add("first",GetNewEmployee("id001"));
    map.Add("second",GetNewEmployee("id002"));
    map.Add("three",GetNewEmployee("id003"));

    DynaAccessUtils.SetProperty(map,"first.Emp_id","new001");
    DynaAccessUtils.SetProperty(map,"second.Emp_id","new002");
    DynaAccessUtils.SetProperty(map,"three.Emp_id","new003");

    Assert.AreEqual("new001",((EmployeeInfo)map["first"]).Emp_id);
    Assert.AreEqual("new002",((EmployeeInfo)map["second"]).Emp_id);
    Assert.AreEqual("new003",((EmployeeInfo)map["three"]).Emp_id);
}

History

14/1/2005 - Initial release.

Contact

Your any feedback and suggestions, please send them to HongJu.Cao (hjcao_wei@hotmail.com).

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

HongJu.Cao
Web Developer
China China
Member
developer located in Tianjin, China.interests include C++/.NET/Java, etc. like coding.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralUse .NET Reflection APIs to facilitate get/set propertygroupChintan.Desai6 Nov '09 - 2:52 
Questionprogramming culture???membervytas z.8 May '08 - 5:56 
QuestionAbout indexer???susscarsonliu1 Sep '05 - 16:25 
a indexer is always a type of "item",when there are two indexer in my class(for example this[int index],this[string name]),when we going to get info. about properties about indexer,it returns two "item" type,when add these two properties into the hashtable,an exception was throw--because key of the hashtable is duplicat
Any solution to solve this problem?
thx advance

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 14 Jan 2005
Article Copyright 2005 by HongJu.Cao
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid