Implement a clone method on MyType, using protected method MemberwiseClone (performs shallow copy) or using a deep cloning technique. You can have it implement an ICloneable then write several extensions methods that will clone the corresponsing collection.
interface ICloneable<T>
{
T Clone();
}
public static class Extensions
{
public static T[] Clone<T>(this T[] array) where T : ICloneable<T>
{
var newArray = new T[array.Length];
for (var i = 0; i < array.Length; i++)
newArray[i] = array[i].Clone();
return newArray;
}
public static IEnumerable<T> Clone<T>(this IEnumerable<T> items) where T : ICloneable<T>
{
foreach (var item in items)
yield return item.Clone();
}
}
You must do this because while a new array is created when you use Array.Copy it copies the references, not the objects referenced. Each type is responsible for copying itself.
It's hard to answer this sort of question precisely without spending an awful lot of time picking your words carefully.
I've done so in a couple of articles which you may find useful:
That's not to say that the articles are perfect, of course - far from it - but I've tried to be as clear as I can.
I think one important thing is to separate the two concepts (parameter passing and reference vs value types) out in your head.
To look at your specific examples:
SomeForm myForm = new SomeForm();
SomeObject myObject = new SomeObject();
myForm.formObject = myObject;
This means that and myForm.formObject refer to the same instance of myObject - like two people having separate pieces of paper, with each one having the same address written on them. If you go to the address on one piece of paper and paint the house red, then go to the address on the second piece of paper, you'll see a red house.SomeObject
It's not clear what you mean by "and then modify the object in the form" because the type you have provided is immutable. There's no way of modifying the object itself. You can change to refer to a different instance of myForm.formObject, but that's like scribbling out the address on one piece of paper and writing a different address on it instead. That won't change what's written on the other piece of paper.SomeObject
If you could provide a short but complete program whose behaviour you don't understand (ideally a console application, just to keep things shorter and simpler) it would be easier to talk about things in concrete terms.
You can use this extension method to copy all object fields and properties.There can be some errors when you try to copy fields and properties,that are reference type.
public static T CopyObject<T>(this T obj) where T : new()
{
var type = obj.GetType();
var props = type.GetProperties();
var fields = type.GetFields();
var copyObj = new T();
foreach (var item in props)
{
item.SetValue(copyObj, item.GetValue(obj));
}
foreach (var item in fields)
{
item.SetValue(copyObj, item.GetValue(obj));
}
return copyObj;
}
Create a new passing the source dictionary in the constructor (of course, this will not copy the objects in the dictionary if they are reference types):Dictionary
var copied = new Dictionary<KeyType, ValueType>(originalDictionary);
public static void CopyPropertiesTo<T, TU>(this T source, TU dest)
{
var sourceProps = typeof (T).GetProperties().Where(x => x.CanRead).ToList();
var destProps = typeof(TU).GetProperties()
.Where(x => x.CanWrite)
.ToList();
foreach (var sourceProp in sourceProps)
{
if (destProps.Any(x => x.Name == sourceProp.Name))
{
var p = destProps.First(x => x.Name == sourceProp.Name);
if(p.CanWrite) { // check if the property can be set or no.
p.SetValue(dest, sourceProp.GetValue(source, null), null);
}
}
}
}
Perform a Deep or Shallow Copy depending on your needs. https://learn.microsoft.com/en-us/dotnet/api/system.object.memberwiseclone?view=netframework-4.8