Skip to content
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

Cannot shim constructor for value type #39

Open
Miista opened this issue Feb 1, 2024 · 1 comment
Open

Cannot shim constructor for value type #39

Miista opened this issue Feb 1, 2024 · 1 comment
Labels
question/investigation Further information or investigation is required

Comments

@Miista
Copy link
Owner

Miista commented Feb 1, 2024

This is evident if attempting the following:

Shim.Replace(() => new DateTime()).With(() => new DateTime(2004, 1, 1));

The above is simply the default constructor. However, GetMethodFromExpression cannot find the constructor for said constructor. It fails in the validation phase because the original method to shim (the constructor) is null. I suspect this is due to it being the default constructor because the following works--insofar as it passes the validation phase.

Shim
    .Replace(() => new DateTime(Is.A<int>(), Is.A<int>(), Is.A<int>()))
    .With((int y, int m, int d) => new DateTime(2004, 1, 1));

The above is no longer the default constructor, and GetMethodFromExpression successfully finds the intended constructor. During the isolation, I can also see that the breakpoint in the replacement is called as expected.
However, the replacement value is never actually present in the resulting code. See the following example.

var s = Shim
    .Replace(() => new DateTime(Is.A<int>(), Is.A<int>(), Is.A<int>()))
    .With((int y, int m, int d) => new DateTime(2004, 1, 1));

    var dateTimeShim = Shim.Replace(() => DateTime.Now).With(() => new DateTime(2004, 1, 1));

    PoseContext.Isolate(()=>
    {
        var dt = new DateTime(2023, 1, 1);
        Console.WriteLine(dt); // <-- This prints '01-01-0001 00:00:00'
    }, s);
@Miista Miista added the question/investigation Further information or investigation is required label Feb 1, 2024
@Miista
Copy link
Owner Author

Miista commented Feb 1, 2024

I've explicitly not labelled this as a bug since I don't know whether this is due to a limitation in the CLR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question/investigation Further information or investigation is required
Projects
None yet
Development

No branches or pull requests

1 participant