创建一个保存属性,然后调用 GetCustomAttributes 不起作用

本文关键字:然后 调用 GetCustomAttributes 不起作用 属性 保存 一个 创建 | 更新日期: 2023-09-27 18:32:21

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{}   

"负载属性"方法中的"属性"变量具有 0 项我做错了什么?

创建一个保存属性,然后调用 GetCustomAttributes 不起作用

这应该有效

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