Amy*_*Amy 7 c# reflection attributes postsharp
考虑以下:
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.ReturnValue)]
public class NotNullAttribute : Attribute
{
}
public class Class1
{
[return: NotNull]
public static string TestMethod([NotNull] string arg)
{
return arg + " + " + arg;
}
}
Run Code Online (Sandbox Code Playgroud)
如何使用System.Reflection,您是否会看到NotNullAttribute属性已应用于方法的返回值?如果你不能,[return:]语法背后的目的是什么?
MethodInfo的有ReturnTypeCustomAttributes属性,如果你调用GetCustomAttributes()这个你得到的返回值atrtibutes.
MethodInfo mi = typeof(Class1).GetMethod("TestMethod");
object[] attrs = mi.ReturnTypeCustomAttributes.GetCustomAttributes(true);
Run Code Online (Sandbox Code Playgroud)