当我运行此代码时,attrs值为空
IEnumerable<object> attrs = ((typeof(Data).GetMethods().Select
(a => a.GetCustomAttributes(typeof(WebGetAttribute),true))));
WebGetAttribute wg= attrs.First() as WebGetAttribute; // wg is null
这是我的课程反映:
public class Data
{
[WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, UriTemplate = "/GetData")]
string GetData(int value)
{
return "";
}
}
我需要帮助才能知道有关WCF服务中每个方法的信息(方法类型/ ResponseFormat / UriTemplate)
答案 0 :(得分:2)
您似乎没有选择NonPublic方法或正确的属性类型。
您可以尝试:
IEnumerable<object> attrs =
typeof(Data).GetMethods(BindingFlags.Public|BindingFlags.NonPublic)
.SelectMany(a => a.GetCustomAttributes(typeof(WebInvokeAttribute),true));
WebInvokeAttribute wi = attrs.First() as WebInvokeAttribute ;
答案 1 :(得分:0)
jbl是正确的。如果没有BindingFlags参数,GetMethods将不会返回非公共方法。此外,由于WebInvokeAttribute不继承WebGetAttribute,因此GetCustomAttributes不会返回它。
以下代码将为所有公共和非公共方法选择WebInvokeAttributes:
IEnumerable<object> attrs = typeof(Data)
.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)
.SelectMany(a => a.GetCustomAttributes(typeof(WebInvokeAttribute), true));