简体   繁体   中英

How to read attribute value from interface AttributeUsage = AttributeTargets.Method

Hello I have an attribute class like:

[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;
        }
    }

I have interface (RequestMethod my enum)

    [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);
        }
    }

And I have attribute reader from here How do I read an attribute on a class at runtime?

 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);
        }
    }

I can't get values from ServiceMethodSettings Attribute. What is wrong with my declaration and how to read values in correct way?

I have also ServiceUrl attribute

[AttributeUsage(AttributeTargets.Interface, AllowMultiple = true, Inherited = true)]
    public class ServiceUrlAttribute : System.Attribute
    {
        public string Url { get; private set; }

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

it working good.

Probably the reason in AttributeUsage AttributeTargets.Method

Thanks for help.

You need to get the attributes from the MethodInfo corresponding to the method eg

MethodInfo method = typeof(IMyService).GetMethod("Search");
ServiceMethodSettingsAttribute attr = (ServiceMethodSettingsAttribute) method.GetCustomAttributes(typeof(ServiceMethodSettingsAttribute), true).FirstOrDefault();

A straightforward change to your method would be to add a parameter for the decorated method name:

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);
}

You could also use expressions instead of specifying the name as a string.

You will have to go to the MethodInfo of your type. Try calling GetMethods() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM