C# set nullable type properties

Date: 2019-10-11
        private static bool IsNullableType(Type type)
        {
            return type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>));
        }

        private static void SetPropertyValueFromProperty(string location, object fromObject, PropertyInfo fromProperty, object toObject, PropertyInfo targetProperty)
        {
            if (fromProperty != null)
            {
                try
                {
                    var propertyValue = fromProperty.GetValue(fromObject, null);
                    if (propertyValue != null)
                    {
                        // Cast type
                        if (propertyValue.GetType() != targetProperty.PropertyType)
                        {

                            var t = propertyValue.GetType();
                            var u = Nullable.GetUnderlyingType(t);
                            var targetType = IsNullableType(targetProperty.PropertyType) ? Nullable.GetUnderlyingType(targetProperty.PropertyType) : targetProperty.PropertyType;

                            if (propertyValue is IConvertible)
                            {
                                propertyValue = Convert.ChangeType(propertyValue, u ?? t);

                                try
                                {
                                    if (targetType.IsEnum)
                                    {
                                        var enumTargetType = Enum.GetUnderlyingType(targetType);
                                        propertyValue = TypeCast(propertyValue, enumTargetType);
                                        //propertyValue = Convert.ChangeType(propertyValue, enumTargetType);
                                    }
                                    else
                                    {
                                        propertyValue = Convert.ChangeType(propertyValue, targetType);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    // Ignore: Can not change type
                                    LoggingHelper.Logger.Debug(ex.Message);
                                }
                            }

                            if (propertyValue.GetType() != targetType)
                            {
                                try
                                {
                                    propertyValue = TypeCast(propertyValue, targetType);
                                }
                                catch (Exception ex)
                                {
                                    // Ignore: Can cast type
                                    LoggingHelper.Logger.Debug(ex.Message);
                                }
                            }
                        }
                    }

                    try
                    {
                        // Dit moet eigenlijk wel altijd goed gaan
                        targetProperty.SetValue(toObject, propertyValue, null);
                    }
                    catch (Exception ex)
                    {

                        LoggingHelper.Logger.Warn(ex.Message);
                        LoggingHelper.Logger.Warn($"{location}: Problem when setting field while using reflection {targetProperty.Name}");
                    }
                }
                catch (Exception ex)
                {
                    // Hier kom je wanneer bepaalde velden niet meer bestaan
                    LoggingHelper.Logger.Warn(ex.Message);
                    LoggingHelper.Logger.Warn($"{location}: Problem with field value while using reflection {targetProperty.Name}");
                }
            }
            else
            {
                LoggingHelper.Logger.Warn($"{location}: Field not found while using reflection {targetProperty.Name}");
            }
        }
public static void SetValue(object inputObject, string propertyName, object propertyVal)
{
    //find out the type
    Type type = inputObject.GetType();
 
    //get the property information based on the type
    System.Reflection.PropertyInfo propertyInfo = type.GetProperty(propertyName);
 
    //find the property type
    Type propertyType = propertyInfo.PropertyType;
     
    //Convert.ChangeType does not handle conversion to nullable types
    //if the property type is nullable, we need to get the underlying type of the property
    var targetType = IsNullableType(propertyInfo.PropertyType) ? Nullable.GetUnderlyingType(propertyInfo.PropertyType) : propertyInfo.PropertyType;
     
    //Returns an System.Object with the specified System.Type and whose value is
    //equivalent to the specified object.
    propertyVal = Convert.ChangeType(propertyVal, targetType);
 
    //Set the value of the property
    propertyInfo.SetValue(inputObject, propertyVal, null);
 
}
private static bool IsNullableType(Type type)
{
    return type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>));
}
26580cookie-checkC# set nullable type properties