Giter Club home page Giter Club logo

Comments (7)

RobSiklos avatar RobSiklos commented on July 29, 2024

I took a quick peek in the code and I believe the solution can be implemented easily. The reason for the problem is that when nullable values are cast to object, it's the underlying value that gets boxed, rather than the nullable instance being referenced. So we've completely lost the fact that an arg is nullable just by looking at its type.

The solution is that in ProtectedMock.ToExpressionArg(), the call to Expression.Constant() should also specify the type (using the other overload for that method).

Here are my modified versions of ToExpressionArg() and ToExpressionArgs():

private static Expression ToExpressionArg(ParameterInfo paramInfo, object arg)
{
    var lambda = arg as LambdaExpression;
    if (lambda != null)
    {
        return lambda.Body;
    }

    var expression = arg as Expression;
    if (expression != null)
    {
        return expression;
    }

    return Expression.Constant(arg, paramInfo.ParameterType);
}

private static IEnumerable<Expression> ToExpressionArgs(MethodInfo method, object[] args)
{
    ParameterInfo[] methodParams = method.GetParameters();
    for (int i = 0; i < args.Length; i++)
    {
        yield return ToExpressionArg(methodParams[i], args[i]);
    }
}

from moq4.

kzu avatar kzu commented on July 29, 2024

Looks good!

Want to send a PR so you get credited appropriately and this fix makes it
into the next release right away?

/kzu from mobile
On Mar 7, 2014 1:42 PM, "Rob Siklos" [email protected] wrote:

I took a quick peek in the code and I believe the solution can be
implemented easily. The reason for the problem is that when nullable values
are cast to object, it's the underlying value that gets boxed, rather
than the nullable instance being referenced. So we've completely lost the
fact that an arg is nullable just by looking at its type.

The solution is that in ProtectedMock.ToExpressionArg(), the call to
Expression.Constant() should also specify the type (using the other
overload for that method).

Here are my modified versions of ToExpressionArg() and ToExpressionArgs():

private static Expression ToExpressionArg(ParameterInfo paramInfo, object arg){
var lambda = arg as LambdaExpression;
if (lambda != null)
{
return lambda.Body;
}

var expression = arg as Expression;
if (expression != null)
{
    return expression;
}

return Expression.Constant(arg, paramInfo.ParameterType);}

private static IEnumerable ToExpressionArgs(MethodInfo method, object[] args){
ParameterInfo[] methodParams = method.GetParameters();
for (int i = 0; i < args.Length; i++)
{
yield return ToExpressionArg(methodParams[i], args[i]);
}}

Reply to this email directly or view it on GitHubhttps://github.com//issues/92#issuecomment-37041881
.

from moq4.

RobSiklos avatar RobSiklos commented on July 29, 2024

@kzu: Unfortunately, I don't have any git integration set up (we're a TFS-based shop) and I don't have time to get it all set up and create a PR. I don't really care about being credited, so if you or someone else would be able to integrate the change, that would be great.

Thanks!

from moq4.

RobSiklos avatar RobSiklos commented on July 29, 2024

@kzu Ok - I finally got around to doing this :) Pull request #199.

from moq4.

RobSiklos avatar RobSiklos commented on July 29, 2024

Pull request #200 (single commit).

from moq4.

RobSiklos avatar RobSiklos commented on July 29, 2024

@kzu Any reason why the pull request hasn't been merged yet?

from moq4.

stakx avatar stakx commented on July 29, 2024

I guess we can close this issue, now that #200 has been merged.

from moq4.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.