I’m doing a audit class. For that I’ve two object of same type, but I don’t know what type.
To audit I just need the properties that make sense in a human reading document.
So I just want the Name (or main identifier) of custom object props.
Example: If I’ve a department and I change the manager. I need to log just the new manager Name. “Manager: John – Bill”
So how can I list just the main properties?
— more about
To define the main identifier I’m using a customAttribute:
[audit]
public string Name { get; set; }
So, I’m thinking in pick them from a list, that except the primitives, with:
var propsOfObject = objectToAudit.GetType().GetProperties();
foreach (PropertyInfo propertyInfo in propsOfObejct) {
IEnumerable<FieldInfo> props = propertyInfo.GetType().GetFields().Where(p => Attribute.IsDefined(p, typeof (AuditAttribute)));
if (props.Count() > 0) {
dicWithNameValue.Add(propertyInfo.GetType().FullName, props.First().GetValue(objectToAudit).ToString());
}
}
right way?
I think it should be like this
FieldInfo.Attributesthat you are trying to use returns anenumof typeFieldAttributesand is used for a different purpose. See msdn.