反射:使用int作为枚举参数的Type.GetMethod [英] Reflection: Type.GetMethod using int for enum parameter
问题描述
你好
我们正在使用设备系统"这里是使用反射来调用方法的.
we are using a "device system" here which is using reflection to invoke methods.
考虑此类:
public class Test
{
public void SetDayOfWeek(DayOfWeek day)
{
//...
}
}
推荐答案
Hi
您必须使用星期几而不是整数,否则您将获得一个空方法信息
You have to use a type of day of the week instead of int else you ll get a null method info
但是您仍然可以传递1作为参数,此代码对我有效并显示星期一
but you still can pass 1 as parameter, this code worked for me and displays Monday
公开
class 测试
class Test
{
public void SetDayOfWeek( DayOfWeek 天)
public void SetDayOfWeek(DayOfWeek day)
{
控制台 .WriteLine(day.ToString());
Console.WriteLine(day.ToString());
}
}
class 程序
class Program
{
静态 void Main( 字符串 [] args)
static void Main(string[] args)
{
MethodInfo m = typeof (命名空间. 测试 ).GetMethod( , BindingFlags .Instance | BindingFlags .Public, 空 ,
MethodInfo m =typeof(Namespace.Test).GetMethod("SetDayOfWeek", BindingFlags.Instance | BindingFlags.Public, null,
新 类型 [] { typeof ( int ) }, 空 );
new Type[] { typeof(int) }, null);
m =
Type .GetType( ).GetMethod( , 新 类型 [] { typeof ( DayOfWeek ) });
Type.GetType("Namespace.Test").GetMethod("SetDayOfWeek", new Type[] { typeof(DayOfWeek) });
测试 t = 新 测试 ();
Test t = new Test();
m.Invoke(t,
m.Invoke(t,
新 对象 [] {1});
new object[] { 1 });
控制台 .Read();
Console.Read();
}
}
这篇关于反射:使用int作为枚举参数的Type.GetMethod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!