使用反射在运行时获取ConditionalAttribute的值

时间:2013-07-29 13:16:13

标签: c# reflection conditional-attribute

我正在开发一个涉及检索给定类型的方法的库。我一直在使用Type.GetMethods,但我注意到了一个问题。假设给定类型中的方法使用ConditionalAttribute,并且此条件的值为false。 GetMethods仍将包含此方法,但我想忽略它。

这是我正在尝试的一个简单示例。这个程序是在调试模式下运行的,所以我想找到一种只忽略fooBar()时调用foo()和fooDebug()的方法。

using System;
using System.Diagnostics;
using System.Reflection;

namespace ConsoleApplication
{
    class ClassA
    {
        public static void foo()
        {
            Console.WriteLine("foo");
        }

        [Conditional("DEBUG")]
        public static void fooDebug()
        {
            Console.WriteLine("fooDebug");
        }

        [Conditional("BAR")]
        public static void fooBar()
        {
            Console.WriteLine("fooBar");
        }
    }

    class Program
    {
    //In this example, I want to find a way where only foo() and fooDebug() are called and fooBar() is ignored, when reflected.
        static void Main(string[] args)
        {
            //Call methods directly.
            //Methods are called/ignored as expected.
            ClassA.foo();//not ignored
            ClassA.fooDebug();//not ignored
            ClassA.fooBar();//ignored

            //Call methods with reflection
            MethodInfo[] methods = typeof(ClassA).GetMethods(BindingFlags.Static | BindingFlags.Public);
            foreach (MethodInfo method in methods)
            {
                //All methods are called, regardless of the ConditionalAttribute.
                method.Invoke(null, null);

                //I figured there would be some workaround like this:
                ConditionalAttribute conditional = 
                Attribute.GetCustomAttribute(method, typeof(ConditionalAttribute)) as ConditionalAttribute;

                if (conditional == null)
                {
                    //The method calls if it has no ConditionalAttribute
                    method.Invoke(null, null);
                }
                else
                {
                    //I can get the string of the condition; but I have no idea how, at runtime, to check if it's defined.
                    string conditionString = conditional.ConditionString;

                    //I also cannnot do a hardcoded (conditionString == "BAR") because the library would not know about BAR
                    bool conditionIsTrue = true;
                    //conditionIsTrue = ??

                    //If the method has a ConditionalAttribute, only call it if the condition is true
                    if (conditionIsTrue)
                    {
                        method.Invoke(null, null);
                    }
                }
            }
        }
    }
}

最后,我想知道哪些方法不包含false ConditionalAttributes。

修改

这个想法适用于其他人会使用的库,因此假设ClassA是他们定义并传递到我的库中的类型。

1 个答案:

答案 0 :(得分:1)

这基本上归结为:“我可以通过反射确定用于编译给定代码的任意条件编译符号” - AFAIK,答案是“不”。


注意:此处的所有内容都不再适用,因为编辑可以清楚地显示此处的上下文是库/调用者。

如果您能够移动方法(并使其非公开),那么可能:

partial class Program
{
    static partial void foo();
    static partial void fooDebug();
    static partial void fooBar();

    static partial void foo()
    {
        Console.WriteLine("foo");
    }

#if DEBUG
    static partial void fooDebug()
    {
        Console.WriteLine("fooDebug");
    }
#endif
#if BAR
    static partial void fooBar()
    {
        Console.WriteLine("fooBar");
    }
#endif
    //In this example, I want to find a way where only foo() and fooDebug() are called and fooBar() is ignored, when reflected.
    static void Main(string[] args)
    {
        //Call methods directly.
        //Methods are called/ignored as expected.
        foo();//not ignored
        fooDebug();//not ignored
        fooBar();//ignored

现在,编译后未实现的方法不存在,因此不会在反射代码中显示。请注意,您现在需要使用BindingFlags.Static | BindingFlags.NonPublic来查找它们,因为它们不是公开的:

MethodInfo[] methods = typeof(Program).GetMethods(BindingFlags.Static | BindingFlags.NonPublic);
foreach (MethodInfo method in methods)
{
    if(method.Name == "Main") continue; // yes, that is lazy
    method.Invoke(null, null);
}