1

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

1
  • 2
    Please read How to Ask and explain what you expect this code to do, what it does or doesn't do and what you've tried to resolve that. Commented Nov 20, 2016 at 9:48

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Furthermore, this class is sealed so it's not possible to extend its behavior (which makes sense, for the reasons you mentioned in your post)
@Bidou Exactly. Entire behavior related to this class is built into the compiler, which leaves no room for customization.
Thanks for the explosion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.