Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello everyone,

I'm want a "clone" my Model to ViewModel using reflection.

But my reflection function cant be reflect Virtual Properties, someone can help me with this?

The code below is my method to "clone" with reflection:

C#
public static D ConvertItem<O, D>(O o)
            where O : class
            where D : class, new()
        {
            D returnValue = new D();

            var typeO = typeof(O);
            var typeD = typeof(D);

            foreach (var item in typeD.GetProperties())
            {
                var prop = typeO.GetProperty(item.Name);

                if (!prop.GetGetMethod().IsVirtual)
                {
                    // NULLABLE PROPERTIES.
                    if (Nullable.GetUnderlyingType(prop.PropertyType) != null)
                    {                        
                        var columnType = prop.PropertyType.GetGenericArguments()[0];

                        if (typeO.GetProperty(item.Name).GetValue(o) != null)
                            item.SetValue(returnValue, Convert.ChangeType(typeO.GetProperty(item.Name).GetValue(o), columnType));
                    }
                    else
                    {
                        // Normal properties.
                        item.SetValue(returnValue, Convert.ChangeType(typeO.GetProperty(item.Name).GetValue(o), item.PropertyType));
                    }
                } 
                else 
                {
                  //HERE, I'AM NEED CALL item.SetValue(...) For VIRTUAL PROPERTIE.
                  //HERE I NEED HELP.
                }

            }
Posted

1 solution

In reflection, you should set property values using the "set" accessor: http://msdn.microsoft.com/en-us/library/system.reflection.propertyinfo.setmethod%28v=vs.110%29.aspx[^].

You get the instance of accessor MethodInfo and work with it like with other methods, that is, invoke it:
http://msdn.microsoft.com/en-us/library/a89hcwhh(v=vs.110).aspx[^].

First parameter is the instance of the object of declaring class (null for a static property), and second parameter is the array of objects with just one object, the value you want to set.

—SA
 
Share this answer
 
Comments
EduChapow 15-Oct-14 20:39pm    
this doesn't not show to me, how i can get value from virtual properties.

public virtual UserIdentity User {get;set;}

Convert.ChangeType(typeO.GetProperty(item.Name).GetValue(o), item.PropertyType));

EduChapow 15-Oct-14 20:47pm    
Message error: "Object should be implements IConvertible."
Sergey Alexandrovich Kryukov 15-Oct-14 20:50pm    
In what line of code? It's on your code, not on mine. You are calling Convert on the object which type does not implement IConvertable. Why would you do that.
I told you how to get the property value (virtual or not). Use my advice.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900