.NET中有没有办法知道哪些参数及其值传递给方法.反思方式?这将在方法内部使用.它必须是通用的,因此可以从任何方法使用它.这是用于记录目的.
Dar*_*rov 11
MethodInfo.GetCurrentMethod()将为您提供有关当前方法的信息,然后使用获取有关参数的信息GetParameters().
使用面向方面的编程可以轻松实现您尝试做的事情.网上有很好的教程,我会指出其中两个:
public void FunctionWithParameters(string message, string operationName = null, string subscriptionId = null)
{
var parameters = System.Reflection.MethodBase.GetCurrentMethod().GetParameters();
this.PrintParams(parameters, message, operationName, subscriptionId);
}
public void PrintParams(ParameterInfo[] paramNames, params object[] args)
{
for (int i = 0; i < args.Length; i++)
{
Console.WriteLine($"{paramNames[i].Name} : {args[i]}");
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14245 次 |
| 最近记录: |