trying to build custom conditional attribute in C#
<p>Considering the default <a href="https://msdn.microsoft.com/en-us/library/system.diagnostics.conditionalattribute.aspx" rel="nofollow noreferrer">conditional attribute</a> in c# is working only with <code>void</code> methods, I'm trying to build my own one.</p>

<p>my code below is availabe at <a href="http://ideone.com/FSOMKi" rel="nofollow noreferrer">http://ideone.com/FSOMKi</a> as well, I got no comiler error, but looks something gone wrong here :(</p>

<pre><code>using System;
using System.Collections.Generic;  // for HashSet
using System.Linq;    // for using Where
using System.Reflection;

namespace attribute
{
public class Test
{
    public static void Main()
    {
        var targetClasses = new HashSet&lt;string&gt;(new[] { "Foo", "Foo2" });
        var targetFns = new HashSet&lt;string&gt;(new[] { "fn", "fn2", "fn3" });

        foreach (var target in targetClasses){
            foreach(var fn in targetFns){
               var method = (target.GetType().GetTypeInfo()) // (typeof(Foo).GetTypeInfo())
                 .DeclaredMethods.Where(x =&gt; x.Name == fn).FirstOrDefault();
                if (method != null) //return 0;
                {
                var customAttributes = (MyCustomAttribute[])method
                                       .GetCustomAttributes(typeof(MyCustomAttribute), true);
            if (customAttributes.Length &gt; 0)
            {
                var myAttribute = customAttributes[0];
                bool value = myAttribute.condition;
                 Console.WriteLine(value);
                if (value == true)
                    method.Invoke(null, null);
                else
                    Console.WriteLine("The attribute parameter is not as required");
            }
                }
        }
        }
    }
}
}


namespace attribute
{
    [AttributeUsage(AttributeTargets.All)]
    public class MyCustomAttribute : Attribute
    {
        public bool condition { get; set; }
    }


    public class Foo
    {
        [MyCustom(condition= true ? ("bar" == "bar") : false)]
        internal static void fn()
        {
            Console.WriteLine("a function in a class");
        }

        [MyCustom(condition= true ? (1 == 2) : false)]
        internal static void fn2()
        {
            Console.WriteLine("another function in the same class");
        }
    }

    public class Foo2
    {
        [MyCustom(condition= true ? (1 == 1) : false)]
        internal static void fn2()
        {
            Console.WriteLine("another function in a nother class");
        }
    }
}
</code></pre>

<p>The output should be three lines as below:</p>

<p>a function in a class
The attribute parameter is not as required
another function in a nother class</p>