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.