Hasan A Yousef
Hasan A Yousef

Reputation: 25008

trying to build custom conditional attribute in C#

Considering the default conditional attribute in c# is working only with void methods, I'm trying to build my own one.

my code below is availabe at http://ideone.com/FSOMKi as well, I got no comiler error, but looks something gone wrong here :(

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<string>(new[] { "Foo", "Foo2" });
        var targetFns = new HashSet<string>(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 => x.Name == fn).FirstOrDefault();
                if (method != null) //return 0;
                {
                var customAttributes = (MyCustomAttribute[])method
                                       .GetCustomAttributes(typeof(MyCustomAttribute), true);
            if (customAttributes.Length > 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");
        }
    }
}

The output should be three lines as below:

a function in a class The attribute parameter is not as required another function in a nother class

Upvotes: 1

Views: 1694

Answers (1)

Zoran Horvat
Zoran Horvat

Reputation: 11321

Your question needs a lot of refactoring.

However, from what I could understand, you are constructing a custom conditional attribute and the compiler doesn't seem to know of it.

The problem in your approach is that the existing ConditionalAttribute is the special case in sense that the C# compiler knows about it. Compiler is taking special compilation action when the method which is invoked is decorated with this attribute.

In that respect, you cannot define another conditional attribute simply because compiler will not be aware of its meaning.

On a related note, ConditionalAttribute cannot be applied to a method returning non-void because its application indicates to the compiler to remove the call to the method. The only kind of method you can skip to call is the method from which you expect nothing - those are methods returning void and having no out parameters.

Upvotes: 3

Related Questions