HB MAAM
HB MAAM

Reputation: 4012

create a save Attributes then call GetCustomAttributes doesn't work

public class MainViewModel
{
    [Save]
    public String Name { get; set; }

    public MainViewModel()
    {
        Name = "qwe asd zxc";
        LoadProperties(this);
    }    

    public void LoadProperties(object viewModel)
    {
         var properties = viewModel.GetType().GetCustomAttributes(typeof(Save),false);
    }
}

public class Save : Attribute{}   

the properties variable in the load properties method has 0 item What did i do wrong?

Upvotes: 0

Views: 156

Answers (1)

L.B
L.B

Reputation: 116168

This should work

var properties = viewModel.GetType()
    .GetProperties()
    .Where(p => p.GetCustomAttributes(typeof(Save),false).Any())
    .ToArray();

Upvotes: 3

Related Questions