Determining if a parameter uses “params” using reflection in C#?

前端 未结 3 495
时光取名叫无心
时光取名叫无心 2021-01-17 07:47

Consider this method signature:

public static void WriteLine(string input, params object[] myObjects)
{
    // Do stuff.
}

How can I determ

相关标签:
3条回答
  • 2021-01-17 08:14

    A slightly shorter and more readable way:

    static bool IsParams(ParameterInfo param)
    {
        return param.IsDefined(typeof(ParamArrayAttribute), false);
    }
    
    0 讨论(0)
  • 2021-01-17 08:24

    Check for the existence of [ParamArrayAttribute] on it.

    The parameter with params will always be the last parameter.

    0 讨论(0)
  • 2021-01-17 08:25

    Check the ParameterInfo, if ParamArrayAttribute has been applied to it:

    static bool IsParams(ParameterInfo param)
    {
        return param.GetCustomAttributes(typeof (ParamArrayAttribute), false).Length > 0;
    }
    
    0 讨论(0)
提交回复
热议问题