Domanda

Ho un metodo in cui sto passando in due oggetti, che hanno gli stessi nomi di proprietà, e sto usando la riflessione per ottenere i valori da un oggetto e impostare i valori dall'altra. Il mio problema è quando mi imbatto in una proprietà che è una raccolta, l'originale è EntityCollection e quello di ottenere set è ObservableCollection, e sto ovviamente intenzione di lanciare un errore di fusione quando si tenta di impostare il valore.

Quindi, come potrei fare per questo? Ho pensato che un modo sarebbe quello di ottenere l'istanza della proprietà EntityCollection e in un Activator.CreateInstance uso loop () per aggiungere un nuovo elemento alla ObservableCollection. Ma mi imbatto in un'altra domanda di come ottenere l'istanza originale.

I sarebbe molto riconoscente per una certa comprensione in questo dilemma. Vi posto il codice per il metodo che sto lavorando con. Attualmente non funziona, soprattutto ho postato solo per un riferimento. Quindi per favore non sottolineare l'ovvio. Grazie in anticipo.

protected void SetValues(Object thisObject, Object entity)
{
    PropertyInfo[] properties = entity.GetType().GetProperties();
    foreach (PropertyInfo property in properties)
    {
        var value = property.GetValue(entity, null);
        var thisObjectsProperty = thisObject.GetType().GetProperty(property.Name);

        if (thisObjectsProperty != null && value != null)
        {
            if (thisObjectsProperty.PropertyType.GetInterface("ICollection", true) != null                && thisObjectsProperty.PropertyType.GetGenericArguments().Count() > 0)
            {
                Type genericType = thisObjectsProperty.PropertyType.GetGenericArguments()[0];
                Type entityCollectionType = property.PropertyType;
                Type thisCollectionType = thisObjectsProperty.PropertyType;

                IList entityCollection = (IList)Activator.CreateInstance(entityCollectionType.MakeGenericType(genericType));
                IList observableCollection = (IList)Activator.CreateInstance(thisCollectionType.MakeGenericType(genericType));

                foreach (var item in entityCollection)
                {
                    String typeString = String.Concat("RulesGenerator.DependencyObjects.", genericType.Name);
                    Type newItemType = Type.GetType(typeString, false, true);
                    if (newItemType != null)
                    {
                        var newItem = Activator.CreateInstance(newItemType);
                        SetValues(newItem, item);
                        observableCollection.Add(newItem);
                    }
                }
            }
            else
                thisObjectsProperty.SetValue(thisObject, value, null);
        }
    }
}
È stato utile?

Soluzione

Implementare l'interfaccia ICloneable di fornire una copia profonda. Si dovrebbe essere in grado di trovare alcuni esempi, ma qui è un punto di partenza:

http: // MSDN. microsoft.com/en-us/library/system.icloneable%28v=VS.71%29.aspx

http://en.csharp-online.net/ICloneable

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;


public class testMain : ICloneable
{

    private string m_TestProp;

    private List<Record> m_Items = new List<Record>();
    public string TestProp
    {
        get { return m_TestProp; }
        set { m_TestProp = value; }
    }

    public List<Record> Items
    {
        get { return m_Items; }
    }


    public object Clone()
    {

        testMain cpy =(testMain) this.MemberwiseClone();

        foreach (Record rec in this.Items)
        {
            Record recCpy = (Record)rec.Clone();
            cpy.Items.Add(recCpy);
        }

        return cpy;
    }

}

public class Record : ICloneable
{

    private string m_TestProp;

    public string TestProp
    {
        get { return m_TestProp; }
        set { m_TestProp = value; }
    }

    public object Clone()
    {
        return this.MemberwiseClone();
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top