1

Well i need some help here i don't know how to solve this problem.

the function of the attribute is to determine if the function can be run...

So what i need is the following:

  1. The consumer of the attribute should be able to determine if it can be executed.
  2. The owner of the attribute should be able to tell the consumer that now it can/can't be executed (like a event).
  3. It must have a simple syntax.

This is what i have so far but it only implements point 1, 3.

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class ExecuteMethodAttribute : Attribute
{
    private Func<object, bool> canExecute;
    public Func<object, bool> CanExecute
    {
        get
        {
            return canExecute;
        }
    }


    public ExecuteMethodAttribute()
    {

    }

    public ExecuteMethodAttribute(Func<object, bool> canExecute)
    {
        this.canExecute = canExecute;
    }
}
1
  • I've run into something similar to this before. You're not going to be able to use this attribute as it is because the parameters to an attribute must be a constant, Type, or Array. Commented Apr 5, 2010 at 16:03

2 Answers 2

5

Attributes are not intended for this type of purpose. You decorate a Type with an attribute, not a specific instance.

If you need a way to specify, to a consumer, that a specific method is ready to execute, or cannot execute, I would rethink your design. Instead of using an attribute, perhaps making a custom class would work much more appropriately.

Given your design, I would recommend taking a look at the ICommand interface. This basically does exactly what you are trying to do. It encapsulates a delegate into a class, with a "CanExecute" method, and an event for notifying "consumers" that the execution availability has changed. This would be a more appropriate option.

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

2 Comments

Well i was playing around with "MVVM" and well i used ICommand first but then i saw that Blend 4 uses Behaviors insead of Commands but then i lost the functionality to specify if it can execute or not, so my idear was to write a Behavior that would give me this functionality...
@Petoj: Blend 4 uses behaviors, but it uses Commands as well. WPF and Silverlight both (now) support ICommand fully, and this can be used in Blend. However, it's not quite as nice as it can be - I have a custom Behavior that lets me bind to an ICommand on the ViewModel, which is nice, since you can use ExecuteCommandAction to trigger it from within Blend with that...
0

I'm afraid attributes won't be suitable for this. I'd rather go for an interface. Something like this:

public interface ICheckedMethod<TReturn, TParam>
{
    TReturn Execute(TParam param);
    bool CanExecute { get; }
    event EventHandler CanExecuteChanged;
}

Then instead of a method you would have a parameter such as this. That is, instead of your approach:

public class MyClass
{
    [ExecuteMethodAttribute(some impossible parameters)]
    public object MyMethod(bool param);
}

You'd write:

public class MyClass
{
    public ICheckedMethod<object, bool> MyMethod { get; private set; }
}

Comments

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.