【发布时间】:2012-04-10 04:13:57
【问题描述】:
我希望能够以IDictionary<string, object> 的形式获取上一个调用方法的参数列表。有一个问题:即使是免费的,我也无法使用第三方面向方面的编程框架。
例如:
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace Question {
internal class Program {
public static void Main(string[] args) {
var impl = new Implementation();
impl.MethodA(1, "two", new OtherClass { Name = "John", Age = 100 });
}
}
internal class Implementation {
public void MethodA(int param1, string param2, OtherClass param3) {
Logger.LogParameters();
}
}
internal class OtherClass {
public string Name { get; set; }
public int Age { get; set; }
}
internal class Logger {
public static void LogParameters() {
var parameters = GetParametersFromPreviousMethodCall();
foreach (var keyValuePair in parameters)
Console.WriteLine(keyValuePair.Key + "=" + keyValuePair.Value);
// keyValuePair.Value may return a object that maybe required to
// inspect to get a representation as a string.
}
private static IDictionary<string, object> GetParametersFromPreviousMethodCall() {
throw new NotImplementedException("I need help here!");
}
}
}
有什么建议或想法吗?如有必要,请随意使用反射。
【问题讨论】:
-
这不需要反射;它需要一个调试器。
标签: c# .net reflection