Printing DisplayName in C#
I have a class Status that has a couple of properties:
Public Class Status
{
[DisplayName("Power"), Description("Index 4: transmit power in dBm")]
public double PwrdBby10
{
get { return (_arrayValue[4] / 10.0); }
}
[DisplayName("Turbo"), Description("Index 5: transmit turbo size and encoding rate")]
public string TurboMsgType
{
get { return GetEnumString(_arrayValue[5], _elements); }
}
}
and I have a reflection function that will compare 2 objects of the same class and print the different properties.
public string Compare(object object1, object object2)
{
var source = object1.GetType();
var propertyNames = source.GetProperties();
var s = new StringBuilder();
foreach (var propertyName in propertyNames)
{
var propertyValue1 = propertyName.GetValue(object1, null);
var propertyValue2 = propertyName.GetValue(object2, null);
if (propertyValue1.ToString() != propertyValue2.ToString())
{
if (s.Length > 0)
s.Append(", ");
s.Append(propertyName.Name);
s.Append("=");
s.Append(propertyValue2.ToString());
}
}
return s.ToString();
}
When I get the result I get the name of the property like "PwrdBpy10 = value"
; instead I want the displayName
to be printed:开发者_Python百科 "Power = value"
.
Can you explain how I can get it to work?
精彩评论