获取属于另一个 class 的 class 的 属性 值
Getting property value of a class that belongs to another class
我有类这样的东西:
public class foo{
public string FooProp1 {get; set;}
public Bar Bar{get; set;}
}
public class Bar{
public string BarProp1 {get; set;}
public string BarProp2 {get; set;}
}
我有一些审计设置,如果我更新 Foo,那么我可以获得 属性 除 'Bar' 之外的所有 属性 的名称和值。有没有办法获取 属性 的名称和 'BarProp1' 的值。
private void ProcessModifiedEntries(Guid transactionId) {
foreach (DbEntityEntry entry in ChangeTracker.Entries().Where(t => t.State == EntityState.Modified).ToList()) {
Track audit = CreateAudit(entry, transactionId, "U");
foreach (var propertyName in entry.CurrentValues.PropertyNames) {
string newValue = entry.CurrentValues[propertyName]?.ToString();
string originalValue = entry.OriginalValues[propertyName]?.ToString();
SetAuditProperty(entry, propertyName, originalValue, audit, newValue);
}
}
}
我想在 Foo 更改时审计 BarProp1。
您希望 classes 向您的审计系统报告附加信息。我认为最好的地方是在您的 CreateAudit
方法中。问题是如何。
你可以有代码为每个传入做一些特殊的事情entry
:
var foo = entry.Entity as Foo;
if (foo != null)
{
// do something with foo.Bar
}
var boo = entry.Entity as Boo;
if (boo != null)
{
// do something with boo.Far
}
等等
当然不是很漂亮。
如果您有多个 classes 需要向审计员报告额外信息,我会定义一个接口并将其添加到每个 classes:
public interface IAuditable
{
string AuditInfo { get; }
}
public class Foo : IAuditable
{
public string FooProp1 { get; set; }
public Bar Bar { get; set; }
[NotMapped]
public string AuditInfo
{
get { return Bar?.BarProp1; }
}
}
然后在CreateAudit
:
var auditable = entry.Entity as IAuditable;
if (auditable != null)
{
// do something with auditable.AuditInfo
}
即使只有一个 class 需要这种行为,我仍然会使用该界面,因为它使您的代码不言自明。
我有类这样的东西:
public class foo{
public string FooProp1 {get; set;}
public Bar Bar{get; set;}
}
public class Bar{
public string BarProp1 {get; set;}
public string BarProp2 {get; set;}
}
我有一些审计设置,如果我更新 Foo,那么我可以获得 属性 除 'Bar' 之外的所有 属性 的名称和值。有没有办法获取 属性 的名称和 'BarProp1' 的值。
private void ProcessModifiedEntries(Guid transactionId) {
foreach (DbEntityEntry entry in ChangeTracker.Entries().Where(t => t.State == EntityState.Modified).ToList()) {
Track audit = CreateAudit(entry, transactionId, "U");
foreach (var propertyName in entry.CurrentValues.PropertyNames) {
string newValue = entry.CurrentValues[propertyName]?.ToString();
string originalValue = entry.OriginalValues[propertyName]?.ToString();
SetAuditProperty(entry, propertyName, originalValue, audit, newValue);
}
}
}
我想在 Foo 更改时审计 BarProp1。
您希望 classes 向您的审计系统报告附加信息。我认为最好的地方是在您的 CreateAudit
方法中。问题是如何。
你可以有代码为每个传入做一些特殊的事情entry
:
var foo = entry.Entity as Foo;
if (foo != null)
{
// do something with foo.Bar
}
var boo = entry.Entity as Boo;
if (boo != null)
{
// do something with boo.Far
}
等等
当然不是很漂亮。
如果您有多个 classes 需要向审计员报告额外信息,我会定义一个接口并将其添加到每个 classes:
public interface IAuditable
{
string AuditInfo { get; }
}
public class Foo : IAuditable
{
public string FooProp1 { get; set; }
public Bar Bar { get; set; }
[NotMapped]
public string AuditInfo
{
get { return Bar?.BarProp1; }
}
}
然后在CreateAudit
:
var auditable = entry.Entity as IAuditable;
if (auditable != null)
{
// do something with auditable.AuditInfo
}
即使只有一个 class 需要这种行为,我仍然会使用该界面,因为它使您的代码不言自明。