mercredi 8 novembre 2017

c# Reflection deep copy set Datetime values

I am trying to deep copy a complex object that has some date properties. I am getting "The value '' could not be converted to a valid date" error. I am using below code for copying:-

if (type.IsPrimitive || type.IsEnum || type == typeof(string))
{
    return obj;
}
else if (type.IsClass || type.IsValueType)
{
    object copiedObject = Activator.CreateInstance(obj.GetType());
    // Get all PropertyInfo.
    PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
    foreach (PropertyInfo property in properties)
    {
        object propertyValue = property.GetValue(obj);
        if (propertyValue != null && property.CanWrite && property.GetSetMethod() != null)
        {
            property.SetValue(copiedObject, CloneProcedure(propertyValue));
        }
    }
}

Am I missing something?





Aucun commentaire:

Enregistrer un commentaire