如何获得程序中使用了某一自定义特性的所有类型

gergul 2021-01-19 11:45:31
如何获得程序中使用了某一自定义特性的所有类型。
例如:
有自定义特性:class MyAttribute: Attribute
我使用这个特性描述了函数foo
那么,我如何在main函数中获得有哪些函数应用了MyAttribute特性?
...全文
234 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
gergul 2021-01-25
  • 打赏
  • 举报
回复
感谢感谢。成功解决我的问题,为你点赞。
m0_54695973 2021-01-20
  • 打赏
  • 举报
回复
您需要收集所有程序集:

ICollection <Assembly> baseAssemblies = AppDomain.CurrentDomain.GetAssemblies();

然後,對於每種類型的收集的程序集,您都可以使用此函數檢索與“ MyAttribute”相關的屬性名稱

公共靜態TValue GetAttributeValue <TAttribute,TValue>(此ICustomAttributeProvider類型,bool collectInheritedAttributes,Func <TAttribute,TValue> valueSelector),其中TAttribute:Attribute
{
var att = type.GetCustomAttributes(typeof(TAttribute),collectInheritedAttributes).FirstOrDefault()作為TAttribute;
如果(att!= null)
{
返回valueSelector(att);
}
返回default(TValue);
}

並以這種方式調用它:

字符串名稱= <collectedType> .GetAttributeValue((MyAttribute dna)=> dna.Name);

再見!
gergul 2021-01-20
  • 打赏
  • 举报
回复
我尝试了一下如上帖,但发现不行,请求支援,谢谢。
引用 1 楼 m0_54695973 的回复:
您需要收集所有程序集: ICollection <Assembly> baseAssemblies = AppDomain.CurrentDomain.GetAssemblies(); 然後,對於每種類型的收集的程序集,您都可以使用此函數檢索與“ MyAttribute”相關的屬性名稱 公共靜態TValue GetAttributeValue <TAttribute,TValue>(此ICustomAttributeProvider類型,bool collectInheritedAttributes,Func <TAttribute,TValue> valueSelector),其中TAttribute:Attribute { var att = type.GetCustomAttributes(typeof(TAttribute),collectInheritedAttributes).FirstOrDefault()作為TAttribute; 如果(att!= null) { 返回valueSelector(att); } 返回default(TValue); } 並以這種方式調用它: 字符串名稱= <collectedType> .GetAttributeValue((MyAttribute dna)=> dna.Name); 再見!
gergul 2021-01-20
  • 打赏
  • 举报
回复
有自定义属性:

[AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = true)]
public class MyAttribute : Attribute
{
    public MyAttribute(int n)
    {
        System.Console.WriteLine("Hello MyAttribute");
    }

    public string _s { get; set; }
    private int _n { get; set; }
}
使用MyAttribute修饰函数foo:
[My(123, _s = "456")]
public void foo()
{
    System.Console.WriteLine("hello foo");
}
遍历MyAttribute修饰过的foo:

List<object> GetAttributeValue<TAttribute>(Type attType, bool collectInheritedAttributes, Func<TAttribute, object> valueSelector) where TAttribute : Attribute
{
    List<object> objs = new List<object>();
    var atts = attType.GetCustomAttributes(typeof(TAttribute), collectInheritedAttributes);
    foreach(var att in atts)
    {
        if (att != null)//这里一次都没有触发
        {
            object o = valueSelector(att as TAttribute);
            if (o != null)
                objs.Add(o);
        }
    }
    return objs;
}

ICollection<System.Reflection.Assembly> baseAssemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (var a in baseAssemblies)
{
    GetAttributeValue<MyAttribute>(a.GetType(), true, at => at);
}
在GetAttributeValue中没有获得MyAttribute属性,我哪里写错了吗? 感谢关注。
m0_54695973 2021-01-20
  • 打赏
  • 举报
回复
您好,我找到了一種更簡單的方法來解決您的問題。 請使用以下代碼:

[AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = true)]
    public class MyAttribute : Attribute
    {
        public MyAttribute(int n)
        {
            System.Console.WriteLine("Hello MyAttribute");
        }

        public string _s { get; set; }
        private int _n { get; set; }
    }


public class MainClass{
        [My(123, _s = "456")]
        public void foo()
        {
            System.Console.WriteLine("hello foo");
        }

        static void Main(string[] args)
        {


            IList<MethodInfo> list = System.AppDomain.CurrentDomain.GetAssemblies()
                .SelectMany(a => a.GetTypes())
                .SelectMany(t => t.GetMethods())
                .Where
                (
                    p => p.GetCustomAttributes(typeof(MyAttribute), true).Any()
                ).ToList();

            foreach (MethodInfo p in list )
            {
                Console.WriteLine(p.Name);
            }
      }
}
讓我知道這是否可以解決您的問題。

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧