Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET: Is there a Class to copy properties of one class to another

Tags:

c#

.net

vb.net

I wrote a function that copies the properties of one class to another so make a copy of an object.

So something like

MyObject myObject = myOtherObject.MyCustomCopy(myObject)

where myObject and myOtherObject are of the same type. I do it by bascually doing

myObject.prop1 = myOtherObject.prop1
myObject.prop2 = myOtherObject.prop2
myObject.prop3 = myOtherObject.prop3
return myObject

I am pretty sure in the past I used a .NET object that automaticaly did this, by reflection I guess, but can't remember it ... or an I imagining that such a method exists?

Yes I'm aware of auto mapper but i was sure (not so much now) that there is a .NET object that does the job. Maybe not!

like image 630
Bob Avatar asked Nov 10 '10 09:11

Bob


People also ask

How do you copy properties from one object to another?

assign(): By using the Object. assign() method, all enumerable properties of one or more source objects are copied to the target object. This method returns the modified target object. The properties of the target object are overwritten if they have the same key as properties in the sources.

How do I copy from one class to another in C#?

MyClass objA = new MyClass(<some parameters>); ... MyClass objB = new MyClass(objA); Here, objB will be an exact copy of objA (with different references), as long as you have implemented the copy constructor correctly, of course.


3 Answers

You may take a look at AutoMapper.

like image 188
Darin Dimitrov Avatar answered Nov 15 '22 18:11

Darin Dimitrov


 public static class ext
{
    public static void CopyAllTo<T>(this T source, T target)
    {
        var type = typeof(T);
        foreach (var sourceProperty in type.GetProperties())
        {
            var targetProperty = type.GetProperty(sourceProperty.Name);
            targetProperty.SetValue(target, sourceProperty.GetValue(source, null), null);
        }
        foreach (var sourceField in type.GetFields())
        {
            var targetField = type.GetField(sourceField.Name);
            targetField.SetValue(target, sourceField.GetValue(source));
        }
    }
}
like image 34
Andreas Avatar answered Nov 15 '22 19:11

Andreas


I know the OP did not ask for a Type to another Type but my variant is one I use for DI in startup.cs for mismatches in configurations between cloud and local dev environment. My local class generally uses a Interface class behind the scenes to map the configurations. Then I use this method to copy properties where they match in name only. I am not checking property types since these are configurations. AutoMapper was suggested. I don't use AutoMapper because we are restricted by U.S. DOD to certain providers. Getting an ATO is hard enough just using .NET, we don't need any more grief.

  using System.Linq;
  public static class PropertyCopy
  {
    public static void CopyAllTo<T,T1>(this T source, T1 target)
    {
      var type = typeof(T);
      var type1 = typeof(T1);
      foreach (var sourceProperty in type.GetProperties())
      {
        foreach (var targetProperty in type1.GetProperties()
          .Where(targetProperty => sourceProperty.Name == targetProperty.Name)
          .Where(targetProperty => targetProperty.SetMethod != null))
        {
          targetProperty.SetValue(target, sourceProperty.GetValue(source, null), null);
        }
      }

      foreach (var sourceField in type.GetFields())
      {
        foreach (var targetField in type1.GetFields()
          .Where(targetField => sourceField.Name == targetField.Name))
        {
          targetField.SetValue(target, sourceField.GetValue(source));
        }
      }

    }
  }
like image 23
Larry Aultman Avatar answered Nov 15 '22 20:11

Larry Aultman