In .net, is there a way using reflection to determine if a parameter on a method is marked with the "params" keyword?
params (C# Reference) By using the params keyword, you can specify a method parameter that takes a variable number of arguments. The parameter type must be a single-dimensional array.
Reflection in C# is used to retrieve metadata on types at runtime. In other words, you can use reflection to inspect metadata of the types in your program dynamically -- you can retrieve information on the loaded assemblies and the types defined in them.
Because the common language runtime (CLR) stores information about the method's name in metadata, reflection must look inside metadata to learn which method on type "D" has the specified name. This logic alone is expensive.
Check to see if ParamArrayAttribute
has been applied to the ParameterInfo
object:
//use string.Format(str, args) as a test
var method = typeof(string).GetMethod("Format", new[] {typeof(string), typeof(object[])});
var param = method.GetParameters()[1];
Console.WriteLine(Attribute.IsDefined(param, typeof(ParamArrayAttribute)));
Test to see whether the final ParameterInfo has ParamArrayAttribute applied to it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With