首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从接口AttributeUsage = AttributeTargets.Method读取属性值

如何从接口AttributeUsage = AttributeTargets.Method读取属性值
EN

Stack Overflow用户
提问于 2013-03-29 13:40:44
回答 2查看 1.9K关注 0票数 0

你好,我有一个属性类,类似于:

代码语言:javascript
复制
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
    public class ServiceMethodSettingsAttribute : Attribute
    {
        public string ServiceName { get; private set; }
        public RequestMethod Method { get; private set; }

        public ServiceMethodSettingsAttribute(string name, RequestMethod method)
        {
            ServiceName = name;
            Method = method;
        }
    }

我有接口(RequestMethod我的枚举)

代码语言:javascript
复制
    [ServiceUrl("/dep")]
    public interface IMyService
    {
        [ServiceMethodSettings("/search", RequestMethod.GET)]
        IQueryable<Department> Search(string value);
    }

 public class MyService : BaseService, IMyService
    {        
        public IQueryable<Department> Search(string value)
        {
            string name = typeof(IMyService).GetAttributeValue((ServiceMethodSettingsAttribute dna) => dna.ServiceName);
            var method = typeof(IMyService).GetAttributeValue((ServiceMethodSettingsAttribute dna) => dna.Method);
        }
    }

我这里有属性读取器How do I read an attribute on a class at runtime?

代码语言:javascript
复制
 public static class AttributeExtensions
    {
        public static TValue GetAttributeValue<TAttribute, TValue>(this Type type, Func<TAttribute, TValue> valueSelector)
            where TAttribute : Attribute
        {
            var att = type.GetCustomAttributes(typeof(TAttribute), true).FirstOrDefault() as TAttribute;
            if (att != null)
            {
                return valueSelector(att);
            }
            return default(TValue);
        }
    }

我无法从ServiceMethodSettings属性中获取值。我的声明有什么问题,以及如何正确地读取值?

我也有ServiceUrl属性

代码语言:javascript
复制
[AttributeUsage(AttributeTargets.Interface, AllowMultiple = true, Inherited = true)]
    public class ServiceUrlAttribute : System.Attribute
    {
        public string Url { get; private set; }

        public ServiceUrlAttribute(string url)
        {
            Url = url;
        }
    }

效果很好。

可能是AttributeUsage AttributeTargets.Method的原因

谢谢你帮忙。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-03-29 13:46:39

您需要从MethodInfo获取与该方法相对应的属性。

代码语言:javascript
复制
MethodInfo method = typeof(IMyService).GetMethod("Search");
ServiceMethodSettingsAttribute attr = (ServiceMethodSettingsAttribute) method.GetCustomAttributes(typeof(ServiceMethodSettingsAttribute), true).FirstOrDefault();

对方法的一个简单更改是为修饰的方法名称添加一个参数:

代码语言:javascript
复制
public static TValue GetMethodAttributeValue<TAttribute, TValue>(this Type type, string methodName, Func<TAttribute, TValue> valueSelector)
    where TAttribute : Attribute
{
    MethodInfo method = type.GetMethod(methodName);
    if(method == null) return default(TValue);
    var att = method.GetCustomAttributes(typeof(TAttribute), true)
        .Cast<TAttribute>()
        .FirstOrDefault();

    if (att != null)
    {
        return valueSelector(att);
    }
    return default(TValue);
}

您也可以使用表达式而不是将名称指定为字符串。

票数 2
EN

Stack Overflow用户

发布于 2013-03-29 13:46:39

你将不得不去你的类型的MethodInfo。试着打电话给GetMethods()

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15704454

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档