Skip to main content
added 11 characters in body
Source Link
Peter Csala
  • 9.8k
  • 1
  • 15
  • 33

Recently I was in need of a method to copy an object and pass it to a method which alters the data. Since a class is passed by reference this would alter the class in the caller which I don't want.

So, I wrote a generic extension method which does the trick. Yet i'm unsure if this is a good solution to my problem. I would like to use reflection to achieve the goal. I can't use ICloneableICloneable and I would like to avoid serializing and deserializing the class. I have also looked into making the classclass a structstruct but will refrain from this since the classes can be way larger than 16 bytes and don't really suit the basic guidelines of when to use a structstruct.

Here is my extension method:

public static T CreateNonReferencedObject<T>(this T obj)
{
    var tObj = (T)Activator.CreateInstance(typeof(T));

    if (obj == null) return tObj;

    var properties = obj.GetType().GetProperties();

    foreach (var property in properties)
    {
        var value = property.GetValue(obj);
        tObj.GetType().GetProperty(property.Name).SetValue(tObj, value);
    }

    return tObj;
}
```

Recently I was in need of a method to copy an object and pass it to a method which alters the data. Since a class is passed by reference this would alter the class in the caller which I don't want.

So I wrote a generic extension method which does the trick. Yet i'm unsure if this is a good solution to my problem. I would like to use reflection to achieve the goal. I can't use ICloneable and I would like to avoid serializing and deserializing the class. I have also looked into making the class a struct but will refrain from this since the classes can be way larger than 16 bytes and don't really suit the basic guidelines of when to use a struct.

Here is my extension method:

public static T CreateNonReferencedObject<T>(this T obj)
{
    var tObj = (T)Activator.CreateInstance(typeof(T));

    if (obj == null) return tObj;

    var properties = obj.GetType().GetProperties();

    foreach (var property in properties)
    {
        var value = property.GetValue(obj);
        tObj.GetType().GetProperty(property.Name).SetValue(tObj, value);
    }

    return tObj;
}
```

Recently I was in need of a method to copy an object and pass it to a method which alters the data. Since a class is passed by reference this would alter the class in the caller which I don't want.

So, I wrote a generic extension method which does the trick. Yet i'm unsure if this is a good solution to my problem. I would like to use reflection to achieve the goal. I can't use ICloneable and I would like to avoid serializing and deserializing the class. I have also looked into making the class a struct but will refrain from this since the classes can be way larger than 16 bytes and don't really suit the basic guidelines of when to use a struct.

Here is my extension method:

public static T CreateNonReferencedObject<T>(this T obj)
{
    var tObj = (T)Activator.CreateInstance(typeof(T));

    if (obj == null) return tObj;

    var properties = obj.GetType().GetProperties();

    foreach (var property in properties)
    {
        var value = property.GetValue(obj);
        tObj.GetType().GetProperty(property.Name).SetValue(tObj, value);
    }

    return tObj;
}
Source Link
Roe
  • 121
  • 1
  • 5

Copy object without reference using reflection

Recently I was in need of a method to copy an object and pass it to a method which alters the data. Since a class is passed by reference this would alter the class in the caller which I don't want.

So I wrote a generic extension method which does the trick. Yet i'm unsure if this is a good solution to my problem. I would like to use reflection to achieve the goal. I can't use ICloneable and I would like to avoid serializing and deserializing the class. I have also looked into making the class a struct but will refrain from this since the classes can be way larger than 16 bytes and don't really suit the basic guidelines of when to use a struct.

Here is my extension method:

public static T CreateNonReferencedObject<T>(this T obj)
{
    var tObj = (T)Activator.CreateInstance(typeof(T));

    if (obj == null) return tObj;

    var properties = obj.GetType().GetProperties();

    foreach (var property in properties)
    {
        var value = property.GetValue(obj);
        tObj.GetType().GetProperty(property.Name).SetValue(tObj, value);
    }

    return tObj;
}
```