Supporting Nullable Property ( int?)

Mar 13, 2014 at 12:11 PM
Hi

I've found a small problem creating a type with nullable properties, for example int?

Copy and paste this code in linqpad to verify the error:
var typeBuilder = new FluentTypeBuilder<object>();
typeBuilder.AddPropertyWithDefaultImplementation("NullableIntProperty", typeof(int?));
var type =typeBuilder.Compile();

var instance = Activator.CreateInstance(type);
This code will throw a ArgumentException: Expression of type 'System.Object' cannot be used for label of type 'System.Nullable`1[System.Int32]'.

I think i was able to fix this small issue with following code change in class FluentBlockBuilder<.....>
private IEnumerable<Expression> _EnumerateExpressions()
{
    foreach (var statement in _statements)
    {
        var expression = statement._CompileToExpression();
        yield return expression;
    }

    if (object.ReferenceEquals(this, _method.Body))
    {
        if (_method.ReturnType == typeof(void))
            yield return Expression.Label(_method._returnTarget);
        else if (_method.ReturnType.IsValueType)
        {
            bool isNullableType = Nullable.GetUnderlyingType(_method.ReturnType) != null;

            if (isNullableType)
            {
                yield return Expression.Label(_method._returnTarget, Expression.Constant(null, _method.ReturnType));
            }
            else
            {
                object value = Activator.CreateInstance(_method.ReturnType);
                yield return Expression.Label(_method._returnTarget, Expression.Constant(value));
            }
        }
        else
            yield return Expression.Label(_method._returnTarget, Expression.Constant(null, _method.ReturnType));
    }
}
The new part is:
bool isNullableType = Nullable.GetUnderlyingType(_method.ReturnType) != null;

if (isNullableType)
{
    yield return Expression.Label(_method._returnTarget, Expression.Constant(null, _method.ReturnType));
}
With this change the code reported above to generate a nullable property works fine.

Let me know if you prefer a pull request.
Hope it helps.

Keep up the good work!
Cheers Massimiliano
Coordinator
Sep 3, 2014 at 12:04 PM
I will look at this problem. I think that I've already corrected a bug related to nullables (maybe it is the same correction that you've done here), but I never published it.
I will probably only update things the next weekend.
Coordinator
Sep 6, 2014 at 5:41 PM
I already corrected the problem with the nullables and signed the assembly.
I am not sure how good is to have a signed assembly with an open source signature, yet it is signed.
I also made the library generate public types, instead of private ones, because dynamic can't work with private types, independently if it already has the instance and the methods are public.

I didn't publish the NuGet package, but that's because I am not the one who created the package and I don't have access to publish it. I already asked Mackenzie Zastrow to publish the package and, if possible, to give me access so I can publish too. I hope this new version solves your problems.