mardi 16 mai 2017

Fastest way to copy objects

I have an object in c# that needs co be copied many times and fast.

Currently I'm doing this with

    public static void CopyFieldsInto<T>(T from, T to,bool ommitNulls=false) where T : new()
    {
        foreach (var f in GetAllFieldsByType<T>())
        {
            var v = f.GetValue(from);
            if (ommitNulls&&v==null)
            {
                continue;
            }
            f.SetValue(to, v);
        }
    }

The problem is that GetValue and SetValue take too long and slow it down.

Is there a better way to do this?





Aucun commentaire:

Enregistrer un commentaire