+ Reply to Thread
Results 1 to 2 of 2

Thread: Mapping object properties

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    35

    Mapping object properties

    What i currently use
    Code:
    public static void MapObjectProperty(object o1, object o2)
        {
            foreach (var p in o1.GetType().GetProperties())
            {
                if (o2.GetType().GetProperty(p.Name) != null)
                {
                    p.SetValue(e1, o2.GetType().GetProperty(p.Name).GetValue(o2, null), null);
                }   
    
            }
        }
    I want to pass a third parameter, a generic list of types that i want to exclude from the mapping. For example strings and booleans. And check if p is of a type in the list

  2. #2
    Junior Member
    Join Date
    Jan 2012
    Posts
    41
    You could use the p.PropertyType property to exclude assignment if the types match exactly.
    Code:
    public static void MapObjectPropertyValues(object o1,
                      object o2,
                      IEnumerable<Type> excludedTypes)
    {
        foreach (var p in o1.GetType().GetProperties())
        {
            if (o2.GetType().GetProperty(p.Name) != null && 
            // next line added
            !(excludedTypes.Contains(p.PropertyType)))
            {
                p.SetValue(o1, o2.GetType().GetProperty(p.Name).GetValue(o2, null), null);
            }
         }
    }

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts