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
Published Friday, September 26, 2008 3:24 PM by anytao

Comments

# Tips: Using Reflection to assign value intelligently - Anytao.net

Pingback from  Tips: Using Reflection to assign value intelligently - Anytao.net

# funny wallpaper &raquo; Tips: Using Reflection to assign value intelligently

Pingback from  funny wallpaper &raquo; Tips: Using Reflection to assign value intelligently

# re: Tips: Using Reflection to assign value intelligently

Friday, September 26, 2008 5:25 AM by zj

支持博客园!

# re: Tips: Using Reflection to assign value intelligently

Friday, September 26, 2008 5:28 AM by zj

支持博客园!

# re: Tips: Using Reflection to assign value intelligently

Friday, September 26, 2008 9:55 AM by anytao

So do I, to supporting our cnblogs.com

# re: Tips: Using Reflection to assign value intelligently

Monday, November 03, 2008 10:36 AM by Rem

The blog is helpfull...

visit also <a href="http://asp-net-example.blogspot.com"><b>asp.net example</b></a>

# re: Tips: Using Reflection to assign value intelligently

Tuesday, December 09, 2008 1:11 AM by szpuxa

vjabR6  <a href="dfqgnwcstwcg.com/.../a>, [url=http://izgybneaadmc.com/]izgybneaadmc[/url], [link=http://aecoeovapcah.com/]aecoeovapcah[/link], http://cfglbcflekfa.com/

# re: Tips: Using Reflection to assign value intelligently

Thursday, December 11, 2008 11:25 AM by punaqggkakf

vJvP3v  <a href="yjqdcqbooaso.com/.../a>, [url=http://jsjbqqmdmugh.com/]jsjbqqmdmugh[/url], [link=http://mikkdtaatcba.com/]mikkdtaatcba[/link], http://ffbiczayvvel.com/

# re: Tips: Using Reflection to assign value intelligently

Monday, February 23, 2009 5:04 AM by Marc Gravell

Note that reflection is very slow if you are talking about large volumes of data. If you have .NET 3.5, perhaps look at MiscUtil (www.yoda.arachsys.com/.../MiscUtil) and the PropertyCopy<T> class; this pre-compiles an `Expression`, to do the assignments much more efficiently. If that isn't an option (.NET 2.0), the look at HyperDescriptor (www.codeproject.com/.../HyperPropertyDescriptor.aspx) - this replaces reflection with pre-compiled dynamic IL when using TypeDescriptor; significantly faster than reflection.

# re: Tips: Using Reflection to assign value intelligently

Monday, February 23, 2009 5:17 AM by anytao

Yes, you are right. Reflection have a slow executing. Your recommand is perfect. And aslo, it's good idea about get value dynmically be expression in .NET 3.5

# re: Tips: Using Reflection to assign value intelligently

Sunday, March 01, 2009 4:57 AM by sdlqksmhuj

TeYbVt  <a href="xjzftacorgxs.com/.../a>, [url=http://pyyuznlpsavg.com/]pyyuznlpsavg[/url], [link=http://ccuiscqsolps.com/]ccuiscqsolps[/link], http://gegrctyoalpo.com/

# re: Tips: Using Reflection to assign value intelligently

Sunday, March 01, 2009 8:37 AM by xmnanpzf

26ha1e  <a href="ofbohzkpcuce.com/.../a>, [url=http://ezdezeyryhfa.com/]ezdezeyryhfa[/url], [link=http://bojptqrnkswe.com/]bojptqrnkswe[/link], http://tzgoiyokeykt.com/

# re: Tips: Using Reflection to assign value intelligently

Friday, March 20, 2009 1:18 PM by deviantART

Good post, admin.

# re: Tips: Using Reflection to assign value intelligently

Friday, March 20, 2009 3:53 PM by Xhizors

Good post, admin.

# re: Tips: Using Reflection to assign value intelligently

Friday, March 20, 2009 6:40 PM by Southwind

Good post, admin.

# re: Tips: Using Reflection to assign value intelligently

Friday, March 20, 2009 9:27 PM by AomiArmster

Good post, admin.

# re: Tips: Using Reflection to assign value intelligently

Saturday, March 21, 2009 12:05 AM by Plornt

Good post, admin.

# re: Tips: Using Reflection to assign value intelligently

Saturday, March 21, 2009 12:06 AM by Plornt

Good post, admin.

# re: Tips: Using Reflection to assign value intelligently

Saturday, March 21, 2009 11:01 AM by Salival

Good post, admin.

# re: Tips: Using Reflection to assign value intelligently

Saturday, March 21, 2009 1:19 PM by Stardust-Poptart

Good post, admin.

# re: Tips: Using Reflection to assign value intelligently

Saturday, March 21, 2009 3:53 PM by vempyrik

Good post, admin.

# re: Tips: Using Reflection to assign value intelligently

Saturday, March 21, 2009 7:46 PM by sangerspyro

Good post, admin.

# re: Tips: Using Reflection to assign value intelligently

Sunday, March 22, 2009 7:46 AM by badfish57

Good post, admin.

# re: Tips: Using Reflection to assign value intelligently

Thursday, March 26, 2009 11:23 AM by Hot

Good site, admin.

# re: Tips: Using Reflection to assign value intelligently

Friday, March 27, 2009 12:05 PM by Flash

Good site, admin.

# re: Tips: Using Reflection to assign value intelligently

Saturday, March 28, 2009 6:13 AM by Panzer

Good site, admin.

# re: Tips: Using Reflection to assign value intelligently

Saturday, March 28, 2009 7:28 AM by Locker

Good site, admin.

# re: Tips: Using Reflection to assign value intelligently

Saturday, March 28, 2009 7:29 AM by Locker

Good site, admin.

# re: Tips: Using Reflection to assign value intelligently

Saturday, March 28, 2009 3:18 PM by Panzer

Good site, admin.

# re: Tips: Using Reflection to assign value intelligently

Saturday, March 28, 2009 5:30 PM by Flash

Good site, admin.

# re: Tips: Using Reflection to assign value intelligently

Saturday, March 28, 2009 6:20 PM by Alex

Good site, admin.

# re: Tips: Using Reflection to assign value intelligently

Thursday, October 22, 2009 6:07 AM by Arnold16

People with mental illness often do  not seek help when they need it. ,

# re: Tips: Using Reflection to assign value intelligently

Friday, October 23, 2009 5:41 AM by Gangster21

Most points on your list are neither new nor a threat to the theory of anthropogenic global warming. ,

Leave a Comment

(required) 
(required) 
(optional)
(required)