Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何获取函数';函数中的MethodInfo?_C#_Reflection - Fatal编程技术网

C# 如何获取函数';函数中的MethodInfo?

C# 如何获取函数';函数中的MethodInfo?,c#,reflection,C#,Reflection,假设有一个函数GetEmployees: public static List<Employee> GetEmployees(Dictionary<int, Department> depts, bool isFullTime) { // How do I get the MethodInfo of this "GetEmployees" function by writing code here? } publicstaticlist Ge

假设有一个函数
GetEmployees

public static List<Employee> GetEmployees(Dictionary<int, Department> depts, bool isFullTime)
{
    // How do I get the MethodInfo of this "GetEmployees" function by writing code here?
}
publicstaticlist GetEmployees(字典部门,bool-isFullTime)
{
//如何通过在此处编写代码来获取此“GetEmployees”函数的MethodInfo?
}
我需要在这个函数中获取MethodInfo的原因是我需要知道

  • 函数名(“GetEmployees”)
  • 参数的数量(“2”)
  • 参数名称(“depts”和“isFullTime”)
  • 参数类型(“System.Collections.Generic.Dictionary`2[int,Department]&”和“System.Boolean”)
  • 返回类型

谢谢

我想你会想要这个:

MethodInfo myMethod = MethodInfo.GetCurrentMethod();
可以得到反射参数,如下所示:

ParameterInfo[] parameters = myMethod.GetParameters();

我想你想要这个:

MethodInfo myMethod = MethodInfo.GetCurrentMethod();
可以得到反射参数,如下所示:

ParameterInfo[] parameters = myMethod.GetParameters();

谢谢你的回复。我忘了问:一旦我有了这个MethodInfo,我如何得到参数的值?你说的“值”是什么意思?您是指调用时传递给
GetEmployees
的值吗?当客户端调用此“GetEmployees”方法时,客户端将某些值传递给参数。调用MethodInfo.GetCurrentMethod()只会获取静态MethodInfo,这表示“此方法有一个名为“isFullTime”的布尔参数,但是我的代码如何获取通过此“isFullTime”传入的“false”“客户端的参数?每次客户端调用此方法时,我的代码都应该被执行。”。它不仅会找到MethodInfo和ParameterInfo,还会找到参数的值。我不确定你能不能做到。谢谢你的回复。我忘了问:一旦我有了这个MethodInfo,我如何得到参数的值?你说的“值”是什么意思?您是指调用时传递给
GetEmployees
的值吗?当客户端调用此“GetEmployees”方法时,客户端将某些值传递给参数。调用MethodInfo.GetCurrentMethod()只会获取静态MethodInfo,这表示“此方法有一个名为“isFullTime”的布尔参数,但是我的代码如何获取通过此“isFullTime”传入的“false”“客户端的参数?每次客户端调用此方法时,我的代码都应该被执行。”。它不仅要找出MethodInfo和ParameterInfo,还要找出参数的值。我不确定你能不能做到这一点。我已经将你的编辑回滚到你的问题,因为编辑完全改变了它的范围,变成了一个不同的问题。如果您想问一个关于如何获取参数值的问题,这是一个新问题。如果您希望将日志添加到所有方法中,您可以使用类似PostSharp(或其他允许的工具)的工具为您注入日志代码。请参阅PostSharp的示例。我已将您的编辑回滚到您的问题,因为编辑完全改变了它的范围,成为一个不同的问题。如果您想问一个关于如何获取参数值的问题,这是一个新问题。如果您希望将日志添加到所有方法中,您可以使用类似PostSharp(或其他允许的工具)的工具为您注入日志代码。有关PostSharp的示例,请参见。