lundi 7 septembre 2015

Copy values between objects

I am writing a utility to copy specific data from a backend SQL database to a client computers SQL Express database. The backend database and the client database are identical. The data are for surveyors that go to remote sites without network. I am using a REST service and using Entity Framework both on the Service and on the proxy. I am copying the property values with this code:

private void GatherFrom<TSelf, TSource>(TSelf self, TSource source)
{
    PropertyInfo[] sourceAllProperties = source.GetType().GetProperties();

    foreach (PropertyInfo sourceProperty in sourceAllProperties)
    {
        PropertyInfo selfProperty = self.GetType().GetProperty(sourceProperty.Name);
        if (selfProperty.CanRead
            && (selfProperty.GetSetMethod(true) != null && !selfProperty.GetSetMethod(true).IsPrivate)
            && (selfProperty.GetSetMethod().Attributes & MethodAttributes.Static) == 0
            && selfProperty.PropertyType.IsAssignableFrom(sourceProperty.PropertyType))
        {
            var sourceValue = sourceProperty.GetValue(source);
            selfProperty.SetValue(self, sourceValue);
        }
    }
}

This works all fine.

But when I apply the new data:

Surveys newSurvey = new Surveys();

GatherFrom(newSurvey, survey);

localSurveys.Add(newSurvey);

I get into problems because I have ambiguous types from remote and local in the same namespace.

Any idea how to split it up?





Aucun commentaire:

Enregistrer un commentaire