代码之家  ›  专栏  ›  技术社区  ›  willeM_ Van Onsem

快速访问在C中保存属性的类型/方法/…。#

  •  8
  • willeM_ Van Onsem  · 技术社区  · 15 年前

    我在这里创建了一个名为a a attribute的自定义属性,例如一个名为b的类,其中一个或多个方法使用该属性。在不遍历整个程序集并查看所有已定义的属性方法的情况下,是否可以获取将属性(在本例中是bmethod1)作为其属性(其中之一)的方法的MethodInfo?它们是否是其他属性目标(参数/类型/属性/…)的模拟方式?我不需要使用该类型属性的所有方法的数组,只需要具有此attribute对象的方法。我想使用它来对方法施加额外的约束(返回类型、参数、名称、其他属性用法…)。

    [AttributeUsage(AttributeTargets.Method)]
    public class AAtribute : Attribute {
    
        //some fields and properties
    
        public AAtribute () {//perhaps with some parameters
            //some operations
            MethodInfo mi;//acces to the MethodInfo with this Attribute
                          //as an Attribute (the question)
            //some operations with the MethodInfo
        }
    
        //some methods
    
    }
    
    public class B {
    
        //some fields, properties and constructors
    
        [A]
        public void BMethod1 () {
            //some operations
        }
    
        //other methods
    
    }
    
    3 回复  |  直到 15 年前
        1
  •  2
  •   Paolo Tedesco    15 年前

    如果我正确理解了你的问题,你想知道, 在属性代码内 ,应用属性的对象(本例中的方法)。
    我很确定没有直接的方法可以做到这一点——属性不知道它所连接的对象,这种关联是相反的。

    我建议你最好的解决方法如下:

    using System;
    using System.Reflection;
    
    namespace test {
    
        [AttributeUsage(AttributeTargets.Method)]
        public class AAttribute : Attribute {
            public AAttribute(Type type,string method) {
                MethodInfo mi = type.GetMethod(method);
            }
        }
    
        public class B {
            [A(typeof(B),"BMethod1")]
            public void BMethod1() {
            }
        }
    }
    

    注释
    通过访问属性的构造函数内的MethodInfo,您希望实现什么?也许还有其他方法可以达到你的目标…

    编辑

    作为另一种可能的解决方案,您可以在属性中提供一个执行检查的静态方法,但这涉及到对MethodInfo进行迭代。

    using System;
    using System.Reflection;
    namespace test {
    
        [AttributeUsage(AttributeTargets.Method)]
        public class AAttribute : Attribute {
            public static void CheckType<T>() {
                foreach (MethodInfo mi in typeof(T).GetMethods()) {
                    AAttribute[] attributes = (AAttribute[])mi.GetCustomAttributes(typeof(AAttribute), false);
                    if (0 != attributes.Length) {
                        // do your checks here
                    }
                }
            }
        }
    
        public class B {
            [A]
            public void BMethod1() {
            }
            [A]
            public int BMethod2() {
                return 0;
            }
        }
    
        public static class Program {
            public static void Main() {
                AAttribute.CheckType<B>();
            }
        }
    }
    
        2
  •  2
  •   Mike Two    15 年前

    我认为答案是否定的,或者至少不合理。只有通过MethodInfo查找属性后,才会构造该属性的实例。实例化具有该属性的方法的类不会实例化该属性。属性实例只有在您开始四处搜索以通过反射找到它们时才会被创建。

        3
  •  0
  •   nexus    15 年前

    要查明方法是否应用了属性,您已经拥有了MethodInfo。

    var type = obj.GetType();
    foreach(var method in type.GetMethods())
    {
        var attributes = method.GetCustomAttributes(typeof(AAtribute));
        if(attributes.Length > 0)
        {
            //this method has AAtribute applied at least once
        }
    }
    推荐文章