0
我使用面向方面的编程来实现日志记录系统。所以,当方法被调用我拦截该呼叫,并获得该功能:比较来自MethodInfo和IMethodInvocation的方法
private IEnumerable<ILogMessage> GetLogMessageFromAttribute(IMethodInvocation input)
{
List<ILogMessage> messages = new List<ILogMessage>();
var t = input.Target.GetType();
var method = t.GetMethods()
.FirstOrDefault(m => m.ToString() == input.MethodBase.ToString());
if (method != null)
{
var attributes = method.CustomAttributes
.Where(atr => atr.AttributeType == typeof(LogAttribute));
foreach (var atr in attributes)
{
var logAttributeInstance =
(LogAttribute)Attribute.GetCustomAttribute(method, typeof(LogAttribute));
messages.Add(MethodInfoTools.FillParametersDataInLogMessage(method, input,
logAttributeInstance.LogMessage));
}
}
return messages;
}
的方法可以通过自己的名字相提并论,但它也可以被重载和属性可以为这些方法不同。
var method = t.GetMethods()
.FirstOrDefault(m => m.ToString() == input.MethodBase.ToString());
这种方式很好,直到我试图拦截通用的方法。在这种情况下的ToString()返回:
System.String TestMethod2[String](System.Collections.Generic.IEnumerable`1[System.String], System.String)
和
System.String TestMethod2[T](System.Collections.Generic.IEnumerable`1[System.String], T)
是否有某种方式来找出什么是执行具体方法?