2012-04-27 1 views
0
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を呼び出すと、負荷のプロパティの方法で変数

答えて

3

これは

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

を動作するはずですと 'MainViewModel' _class_の属性ではなく、そのプロパティの属性から、検査されているので、元のコードは動作しません_why_理由は、あります。 –

関連する問題