我为模型的每个属性定制了自定义属性
[Auditable]
public class Student : BaseObject
{
public Student()
// Change this parameter to change the DisplayName
// (this name is used in all system messages)
// property of this object
: base("Student") { }
public int StudentId { get; set; }
[Auditable(false)]
public int OfficeAddressId { get; set; }
保存学生记录时,我想检查可审核属性并将其写入audittrack表。我想在
中这样做 private void CurrentObjectContext_SavingChanges(object sender, EventArgs e)
{
有人可以指示我使用Auditable(false)访问该属性,而其他人则可以访问每个实体。
答案 0 :(得分:2)
您可以使用此LINQ表达式获取sender
使用Auditable
属性注释的所有属性(提供的IsAuditable
属性为Auditable
属性,其中值为从构造函数存储)
var auditableProperties = from p in sender.GetType().GetProperties()
let attribute = p.GetCustomAttributes(typeof(AuditableAttribute), false).SingleOrDefault() as AuditableAttribute
where attribute != null && attribute.IsAuditable == true
select p;
表达式的结果是PropertyInfo
个对象的集合。