0

こんにちは、次のような属性クラスがあります。

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

ここから属性リーダーがあります 実行時にクラスの属性を読み取るにはどうすればよいですか?

 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 属性もあります

[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 の理由

手伝ってくれてありがとう。

4

2 に答える 2

2

MethodInfoメソッドに対応する属性を取得する必要があります。

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

メソッドへの簡単な変更は、装飾されたメソッド名のパラメーターを追加することです。

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

名前を文字列として指定する代わりに、式を使用することもできます。

于 2013-03-29T13:46:39.273 に答える
1

あなたのタイプのに行く必要がMethodInfoあります。に電話してみてくださいGetMethods()

于 2013-03-29T13:46:39.433 に答える