Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Attribute 'Dependency' is not valid on this declaration type." error

Why I receive such message?

Attribute 'Dependency' is not valid on this declaration type. It is only valid on 'assembly' declarations.

public partial class MainWindow : Window
{
    private OverviewViewModel _vm;

    [Dependency]
    public OverviewViewModel VM
    {
        set
        {
            _vm = value;
            this.DataContext = _vm;
        }
    }
like image 846
Oleg Vazhnev Avatar asked Nov 20 '11 18:11

Oleg Vazhnev


2 Answers

You are probably using the wrong attribute: DependencyAttribute

Indicates when a dependency is to be loaded by the referring assembly [...]

and can only be applied to assemblies (and not to properties like you are trying), e.g.:

[assembly: Dependency(/*...*/)]
like image 171
Matthias Avatar answered Nov 06 '22 09:11

Matthias


Attributes are allowed to declare what they can apply to (via AttributeUsageAttribute). The default is anything, but in this case it is "assembly", meaning: you can only apply this at the assembly level, which you do via:

[assembly:Dependency(...)]

If this is your own attribute, check the AttributeUsageAttribute associated with it, and ensure it includes properties (using the pipe | to apply "or").

If it is not your attribute, double-check the intended usage - you might be using it wrong.

like image 27
Marc Gravell Avatar answered Nov 06 '22 09:11

Marc Gravell