Cast object to another object generic

Cast object to another object generic having properties in common. using extension method (Generic)
This extension method can be used to cast one object to another object. objects should have common properties. i-e classes with different names but with same properties. For example in MVC if you use View model and actual model (pocco classes). you can cast view model to actual model.

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Web;


public static class Extensions
    {
   
        private static bool IsNullableType(Type type)
        {
            return type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>));
        }
        public static bool HasProperty(this object obj, string propertyName)
        {
            return obj.GetType().GetProperty(propertyName) != null;
        }
    /// <summary>
    /// Cast the object to target
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="myobj"></param>
    /// <returns>Target Object</returns>
        public static T Cast<T>(this object myobj) where T : new()
        {
            Type objectType = myobj.GetType();
            Type target = typeof(T);
            var x = Activator.CreateInstance(target, false);
            var z = from source in objectType.GetMembers().ToList()
                    where source.MemberType == MemberTypes.Property
                    select source;
            var d = from source in target.GetMembers().ToList()
                    where source.MemberType == MemberTypes.Property
                    select source;
            List<MemberInfo> members = d.Where(memberInfo => d.Select(c => c.Name)
               .ToList().Contains(memberInfo.Name)).ToList();
            PropertyInfo propertyInfo;
            object value;
            foreach (var memberInfo in members)
            {
                propertyInfo = typeof(T).GetProperty(memberInfo.Name);
                if (myobj.GetType().GetProperty(memberInfo.Name) != null)
                {
                    value = myobj.GetType().GetProperty(memberInfo.Name).GetValue(myobj, null);
                    propertyInfo.SetValue(x, value, null);
                }
            }
            return (T)x;
        }
 
    }

1 Comments

  1. This way my pal Wesley Virgin's biography launches in this SHOCKING and controversial video.

    Wesley was in the army-and shortly after leaving-he unveiled hidden, "mind control" secrets that the CIA and others used to get everything they want.

    As it turns out, these are the same SECRETS tons of celebrities (notably those who "come out of nowhere") and the greatest business people used to become rich and successful.

    You probably know that you only use 10% of your brain.

    That's really because the majority of your brainpower is UNCONSCIOUS.

    Perhaps this conversation has even taken place INSIDE your very own brain... as it did in my good friend Wesley Virgin's brain around seven years ago, while riding an unregistered, beat-up bucket of a car without a license and $3.20 in his pocket.

    "I'm so frustrated with living check to check! Why can't I become successful?"

    You've taken part in those thoughts, right?

    Your success story is waiting to be written. You just have to take a leap of faith in YOURSELF.

    CLICK HERE TO LEARN WESLEY'S METHOD

    ReplyDelete

Post a Comment