0

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.

Community
  • 1
  • 1
Arbejdsglæde
  • 13,670
  • 26
  • 78
  • 144

2 Answers2

2

You need to get the attributes from the MethodInfo corresponding to the method e.g.

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.

Lee
  • 142,018
  • 20
  • 234
  • 287
1

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

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445