Tips: Using Reflection to assign value intelligently

Published 26 Sept 2008, by Anytao
© 2008 Anytao.com, A fun world for making tech as art.  

Usually, we map data from DB into models in for DAL. Of cource, we aslo encapsulate the models into Business Object(BO) or Domain Object(DO) for BLL. That's usual design affairs. In the case of design, different layers make a clear organize about business logic from data, service and so on.

However, it's unavoidable to pass value from Models to BOs to run business logic or pass value from BOs to Models to save data. This is unreasonable to handle many data transfer but necessary in this satuiation. How can we make it easy? Of course it is. Refelecting is a useful way to make data transfer easy for different CLR types value assignment.

Here is the way to get value from a instance by its propertyName:

        private static object GetPropertyValue(object instance, string propertyName)
        {
            
return instance.GetType().InvokeMember(propertyName, BindingFlags.GetProperty,
                
null, instance, new object[] { });
        }
 
And, here is the way to set value to a instance by its propertyName
        private static void SetPropertyValue(object instance, string propertyName, object propertySetValue)
        {
            instance.GetType().InvokeMember(propertyName, BindingFlags.SetProperty,
                
null, instance, new object[] { Convert.ChangeType(propertySetValue, propertySetValue.GetType()) });
        }
 
So, if you have a instance need to transfer its value to another different type instance such as BO and model. You can easily to use refelection to solve your trouble. Of course, this case is based on the two instance have the same property name. CLR can't resolve the assign rules, you need to make this action all by yourself. Finally, you can use refelection get propery name easily, like
        public static List<PropertyInfo> GetProperties<T>()
        {
            List
<PropertyInfo> list = new List<PropertyInfo>();

            Type type 
= typeof(T);

            
object[] oProperties = type.GetProperties();

            
foreach (PropertyInfo pi in oProperties)
            {
                list.Add(pi);
            }

            
return list;
        }
 
Finally, generic technology can encapsulate your value assignment more intelligent, type can be resolved in runtime. OK, try your code helper from here,
 
            public void Anytao.CodeHelper.AssignValue<Tgetter, Tsetter>(Tgetter getter, Tsetter setter)
            {
                
//Your value assgin logic
            }
 
Hope this helps,
Tao | Inside Necessary .NET www.anytao.com  | Blog: http://anytao.cnblogs.com/Tao Wang is Senior Developer working on a chinese company and responsible for project framework design, software develop and management. Tao is proficient with CLR Essential, good command of ASP.NETADO.NETXMLSQL Server, and skilled in object-oriented, design pattern. Here is my new book about .NET inside

9 Comments

Comments have been disabled for this content.