Click here to Skip to main content
15,881,248 members
Articles / Programming Languages / C#
Article

Use .NET Reflection APIs to facilitate get/set property

Rate me:
Please Sign up or sign in to vote.
2.63/5 (10 votes)
13 Jan 20051 min read 42.7K   488   12   3
Dynamic access to .NET object's property/field.

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:

C#
[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:

C#
[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:

C#
[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


Written By
Web Developer
China China
developer located in Tianjin, China.interests include C++/.NET/Java, etc. like coding.

Comments and Discussions

 
GeneralUse .NET Reflection APIs to facilitate get/set property Pin
Chintan.Desai6-Nov-09 2:52
Chintan.Desai6-Nov-09 2:52 
Questionprogramming culture??? Pin
vytas z.8-May-08 5:56
vytas z.8-May-08 5:56 
QuestionAbout indexer??? Pin
carsonliu1-Sep-05 16:25
carsonliu1-Sep-05 16:25 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.