简体   繁体   中英

Why repassing dynamic parameter to params parameter fails to build

When repassing a dynamic parameter to a method with params keyword the solution fails to build, I am using .NET 4.6 and VS2015, but the problem also happens with previous versions of .NET Framework. No error is produced on Error List, just a "Build Failed" message at the bottom.

This is the sample code I am trying to run:

public void MethodWithDynamicParameter(dynamic dyn)
{
    MethodWithParams(dyn); //This fails to build!

    MethodWithParams(new object[] { dyn }); //This compiles!
}

public void MethodWithParams(params object[] objects)
{

}

Can someone explain what is wrong with the first call?

EDIT 1:

I´ve created a new solution with the sample provided by Dave and it builds with no problems. But in my solution the problems persists even after "Close, clean and build". It does not matter if I pass a string, a object, a dynamic or anythin else. At the image below there are no calls to method and the solution still does not build.

在此处输入图片说明

I have created a new Console Application using the following code as a test:

class Program
{
    static void Main(string[] args)
    {
        // Test with object:
        object x = new object();
        MethodWithDynamicParameter(x);

        // Test with specific type of object, a string:
        MethodWithDynamicParameter("string");

        Console.ReadKey();
    }

    static void MethodWithDynamicParameter(dynamic dyn)
    {
        MethodWithParams(dyn);
        MethodWithParams(new object[] { dyn });
    }

    static void MethodWithParams(params object[] objects)
    {

    }
}

For me, the program both compiles and runs without error.

May I suggest the standard "close all documents, clean all, rebuild all" and/or "restart Visual Studio" solutions, if you have not yet attempted this?


Additionally, they dynamic keyword might be the source of your troubles, as it bypasses many of the typing checks until compile time. Try checking your code from where dyn would be declared, prior to making the call to MethodWithDynamicParameter(dynamic dyn) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM