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

InvalidCastException thrown from InvokeConstructor if passing more than 14 params arguments #3

Open
jbtule opened this issue Feb 4, 2014 · 3 comments
Assignees

Comments

@jbtule
Copy link
Member

jbtule commented Feb 4, 2014

from ekonbenefits/impromptu-interface#9: by @jdh28

I have a class that I'm creating dynamically via Dynamic.InvokeConstructor that has params arguments. If I pass in 14 arguments it works, but 15 causes an InvalidCastException:

System.InvalidCastException: The result type 'MyClass' of the dynamic binding produced by binder 'Microsoft.CSharp.RuntimeBinder.CSharpInvokeConstructorBinder' is not compatible with the result type 'System.Type' expected by the call site.

Here is some example code:

    class Program
    {
        static void Main(string[] args)
        {
            var parameters = Enumerable.Range(0, 15).Select(i => i.ToString() as object).ToArray();
            var instance = Dynamic.InvokeConstructor(typeof(MyClass), parameters);
            Console.Out.WriteLine(instance.Args);
        }
    }


    public class MyClass
    {
        public readonly string Args;

        public MyClass(params string[] args)
        {
            Args = String.Join(",", args);
        }
    }
@jbtule
Copy link
Member Author

jbtule commented Feb 4, 2014

This is certainly a bug. A workaround is:

var parameters = Enumerable.Range(0, 15).Select(i => i.ToString()).ToArray();
var instance = Dynamic.InvokeConstructor(typeof(MyClass), new object []{ parameters});
Console.Out.WriteLine(instance.Args);

But I'll need to look into fixing this.

@jbtule jbtule self-assigned this Feb 4, 2014
@jdh28
Copy link

jdh28 commented Feb 4, 2014

The workaround doesn't seem to work for me; I'd have to cast my parameters to 'string' rather than 'object'. I also need to be able to construct types with signatures like:

public MyClass(string firstArg, params string[] otherArgs)

and I was relying on the library to sort that out for me.

@jbtule
Copy link
Member Author

jbtule commented Feb 4, 2014

this seems to be related to emitting the callsite delegate for constructors above 14 arguments
InvokeHelper.cs#L468

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants