Skip to content

NH-3290 fixes setting a uint target type, like uint flag fields, using an int value. #153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion src/NHibernate/Transform/AliasToBeanResultTransformer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using NHibernate.Properties;

Expand Down Expand Up @@ -32,6 +33,8 @@ public class AliasToBeanResultTransformer : IResultTransformer
private ISetter[] setters;
private readonly IPropertyAccessor propertyAccessor;
private readonly ConstructorInfo constructor;
private Dictionary<string, System.Type> propertyTypes;
private Dictionary<string, System.Type> fieldTypes;

public AliasToBeanResultTransformer(System.Type resultClass)
{
Expand All @@ -57,6 +60,24 @@ public AliasToBeanResultTransformer(System.Type resultClass)
PropertyAccessorFactory.GetPropertyAccessor(null),
PropertyAccessorFactory.GetPropertyAccessor("field")
});

// Get and store the public and non-public type for the instance properties
var properties = resultClass.GetProperties(flags);
propertyTypes = new Dictionary<string, System.Type>(properties.Length);

foreach (var property in properties)
{
propertyTypes.Add(property.Name, property.PropertyType);
}

// Get and store the public and non-public type for the instance fields
var fields = resultClass.GetFields(flags);
fieldTypes = new Dictionary<string, System.Type>(fields.Length);

foreach (var field in fields)
{
fieldTypes.Add(field.Name, field.FieldType);
}
}

public object TransformTuple(object[] tuple, String[] aliases)
Expand Down Expand Up @@ -91,7 +112,35 @@ public object TransformTuple(object[] tuple, String[] aliases)
{
if (setters[i] != null)
{
setters[i].Set(result, tuple[i]);
System.Type type = null;
var value = tuple[i];

if (propertyTypes.ContainsKey(aliases[i]))
{
type = propertyTypes[aliases[i]];
}
else if (fieldTypes.ContainsKey(aliases[i]))
{
type = fieldTypes[aliases[i]];
}

var valueType = value.GetType();

// NH-3290 fix works for convertible types
if (type != null && typeof(IConvertible).IsAssignableFrom(valueType))
{
if (type.IsEnum)
{
type = Enum.GetUnderlyingType(type);
}

if (type != valueType)
{
value = Convert.ChangeType(value, type);
}
}

setters[i].Set(result, value);
}
}
}
Expand Down