![](/img/trans.png)
[英].NET Reflection - How to get “real” type from out ParameterInfo
[英]How to get real value from the ParameterInfo[] in Reflection
实际上,我有几个问题可以更容易地理解我在问什么我必须先展示我的代码。
public static void Main(string[] args)
{
CustomClass customClass = new CustomClass();
customClass.SomeMethod("asdas", true, 30.5);
}
public class CustomClass
{
[MyAttribute]
public Boolean SomeMethod(String a, Boolean b, Double c)
{
return true;
}
}
public class MyAttribute : Attribute
{
public MyAttribute()
{
SomeIntercepter.InterceptEverything();
}
public void DoSomethingBeforeMethodexecutes()
{
....
}
public void DoSomethingAfterMethodExecutes()
{
....
}
}
public class SomeIntercepter
{
public static void InterceptEverything()
{
StackTrace stackTrace = new StackTrace();
var method = stackTrace.GetFrame(2).GetMethod();
var parameters = method.GetParameters();
if (parameters.Length > 3)
return;
String cacheKey = method.Name;
for (int i = 0; i < parameters.Length; i++)
{
//HOW TO GET THE PARAMETER DATA ASSIGNED
cacheKey += "_" + parameters[i];
}
}
}
所以我尝试做的。 我尝试在它调用的每个方法上拦截方法,并根据标记为[MyAttribute]
的方法的传入数据执行某些操作。 因此,我通过 StackTrace 访问该方法并尝试使用 GetParameters 获取所有传入数据。 这是我的问题:1) 如何在 InterceptEverything() 中对SomeMethod
的所有SomeMethod
数据进行对象 [] 2) 如何在使用MyAttribute
标记的方法之前运行MyAttribute
以运行方法DoSomethingBeforeMethodexecutes()
3) 如何要说到MyAttribute
与标记方法后运行MyAttribute
运行方法DoSomethingAfterMethodexecutes()
感谢您的任何建议。
您无法通过ParameterInfo
从任何特定帧获取值。 该设施不存在。
另外:属性不执行。 除非您使用 postsharp 之类的东西,否则它们只是信息。 要让它们执行,您必须显式编写反射代码来执行此操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.