Replies: 4 comments 26 replies
-
Additional metadata isn't necessary in the case of local functions as the member isn't accessible outside of the assembly and the compiler is well aware that it accepts params. Why would you need to know this through reflection? How the method is encoded should be treated as implementation details and anything not explicitly stated in the specification should never be relied upon. |
Beta Was this translation helpful? Give feedback.
-
If you want to check if a delegate has a params array, you should be checking the |
Beta Was this translation helpful? Give feedback.
-
I'd like to add that attributes are usually included for local function arguments (just not using System;
using System.Reflection;
using System.ComponentModel;
public class Program {
public static void Main() {
static void MethodA([DefaultValue("Default")] object[] Args) {
}
static void MethodB(params object[] Args) {
}
Console.WriteLine(((Delegate)MethodA).Method.GetParameters()[0].IsDefined(typeof(DefaultValueAttribute)));
Console.WriteLine(((Delegate)MethodB).Method.GetParameters()[0].IsDefined(typeof(ParamArrayAttribute)));
Console.ReadLine();
}
} Output:
|
Beta Was this translation helpful? Give feedback.
-
Thanks to @HaloFour for the workaround, but it only works with using System;
using System.Reflection;
using System.ComponentModel;
public class Program {
static void ClassMethod(params object[] Args) {
}
static void ClassMethod2([DefaultValue("Default")] object[] Args) {
}
public static void Main() {
Console.WriteLine("Local ParamArrayAttribute");
static void LocalMethod(params object[] Args) {
}
Console.WriteLine(((Delegate)LocalMethod).GetType().GetMethod("Invoke").GetParameters()[0].IsDefined(typeof(ParamArrayAttribute)));
Console.WriteLine(((Delegate)LocalMethod).Method.GetParameters()[0].IsDefined(typeof(ParamArrayAttribute)));
Console.WriteLine("\nLocal DefaultValueAttribute");
static void LocalMethod2([DefaultValue("Default")] object[] Args) {
}
Console.WriteLine(((Delegate)LocalMethod2).GetType().GetMethod("Invoke").GetParameters()[0].IsDefined(typeof(DefaultValueAttribute)));
Console.WriteLine(((Delegate)LocalMethod2).Method.GetParameters()[0].IsDefined(typeof(DefaultValueAttribute)));
Console.WriteLine("\nClass ParamArrayAttribute");
Console.WriteLine(((Delegate)ClassMethod).GetType().GetMethod("Invoke").GetParameters()[0].IsDefined(typeof(ParamArrayAttribute)));
Console.WriteLine(((Delegate)ClassMethod).Method.GetParameters()[0].IsDefined(typeof(ParamArrayAttribute)));
Console.WriteLine("\nClass DefaultValueAttribute");
Console.WriteLine(((Delegate)ClassMethod2).GetType().GetMethod("Invoke").GetParameters()[0].IsDefined(typeof(DefaultValueAttribute)));
Console.WriteLine(((Delegate)ClassMethod2).Method.GetParameters()[0].IsDefined(typeof(DefaultValueAttribute)));
}
} Output (.NET 8):
|
Beta Was this translation helpful? Give feedback.
-
I'm trying to check whether an argument for a function is
params
using reflection. It seems like the only way to do this is find aParamArrayAttribute
. However, it seems this is not generated for local functions! Is there another way?Output (tested only for .NET 7 and .NET 8):
Beta Was this translation helpful? Give feedback.
All reactions