简体   繁体   中英

Overloading functions, which have format and params args, with new string argument

I have such a function and its overload:

  1. SomeMethod(String format, params Object[] prms)
  2. SomeMethod(String str, Exception ex, String format, params Object[] prms)

The problem is that when I use second variant of the signature my application decides to go through the first and uses "str" as format. Can I somehow avoid this without changing params array type (in my situation I have no opportunity to do it)?

It seems that if parameter two is of type Exception then second overload is preferred as conversion Exception -> Exception is better than Exception -> Object . @MatthewWhited noted that you can pass in named parameters to force one or the other overload to be called.

7.5.3.2 Better function member C# 4 specificatipn

For the purposes of determining the better function member, a stripped-down argument list A is constructed containing just the argument expressions themselves in the order they appear in the original argument list. Parameter lists for each of the candidate function members are constructed in the following way:
• The expanded form is used if the function member was applicable only in the expanded form.
• Optional parameters with no corresponding arguments are removed from the parameter list
• The parameters are reordered so that they occur at the same position as the corresponding argument in the argument list.
Given an argument list A with a set of argument expressions { E1, E2, ..., EN } and two applicable function members MP and MQ with parameter types { P1, P2, ..., PN } and { Q1, Q2, ..., QN }, MP is defined to be a better function member than MQ if
• for each argument, the implicit conversion from EX to QX is not better than the implicit conversion from EX to PX, and
• for at least one argument, the conversion from EX to PX is better than the conversion from EX to QX.

The following code calls method #1, #1, #1, #2, #2

public class test_params
{
    public void SomeMethod(String format, params Object[] prms) { Console.Write("#1: ");  Console.WriteLine(format, prms); }
    public void SomeMethod(String str, Exception ex, String format, params Object[] prms) { Console.Write("#2 "); Console.WriteLine("str={0} Excep={1}", str, ex.Message); Console.WriteLine(format, prms); }
    public void Test()
    {
        SomeMethod("", 1, 2, 3);                                            // #1:
        SomeMethod("{0} {1}", 1, 2);                                        // #1: 1 2
        SomeMethod("{0} {1}", 1, "p2", 3, 4);                               // #1: 1 p2
        SomeMethod("{0} {1}", new Exception("Test excep"), "p2");           // #2 str={0} {1} Excep=Test excep // p2
        SomeMethod("Str1", new Exception("Test excep"), "{0} {1}", 1, 2);   // #2 str=Str1 Excep=Test excep // 1 2
    }
}

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