1

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

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

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

最终,我想知道哪些方法不包含错误的 ConditionalAttributes。

编辑

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

4

1 回答 1

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);
}
于 2013-07-29T13:21:52.770 回答