Note that there are some explanatory texts on larger screens.

plurals
  1. POGet value of ConditionalAttribute at runtime using reflection
    text
    copied!<p>I am working on a library that involves retrieving the methods of a given type. I have been using <code>Type.GetMethods</code>, but I've noticed an issue. Let's say that a method in the given type uses a <code>ConditionalAttribute</code>, and the value for this condition is false. GetMethods will still include this method, but I would like to ignore it.</p> <p>Here is a simple example of what I am attempting. This program is ran in Debug mode, so I want to find a way where only foo() and fooDebug() are called while fooBar() is ignored.</p> <pre><code>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); } } } } } } </code></pre> <p>Ultimately, I would like to know which methods do not include false ConditionalAttributes. </p> <p><strong>EDIT</strong></p> <p>This idea is for a library that others would use, so assume that ClassA is a type that they define and pass into my library.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload