ウィリー はあなたに
に関して、 に感謝し、助けてください。このよう
public string Compare<T>(T newVal, T oldVal)
{
StringBuilder retVal = new StringBuilder();
Type objectsType = typeof(T);
foreach (PropertyInfo propertyInfo in objectsType.GetProperties(
BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance))
{
//if (?) --> how to identify the propertyInfo here as class so that i can iterate using recursive
//{
// Compare(propertyInfo.GetValue(oldVal, null), propertyInfo.GetValue(newVal, null));
//}
if (propertyInfo.CanRead)
{
object newValue = propertyInfo.GetValue(newVal, null);
object oldValue = propertyInfo.GetValue(oldVal, null);
if (propertyInfo.PropertyType.Namespace.StartsWith("System") && !propertyInfo.PropertyType.IsGenericType)
{
if (!Object.Equals(newValue, oldValue))
{
retVal.Append(propertyInfo.Name + " = " + newValue.ToString() + ";");
}
}
}
}
return retVal.ToString();
}
です特性を比較することができた。私がユーザークラスを変更するとします。
public class BaseEntity
{
private string _createdBy;
private DateTime _createdDate;
private string _updatedBy;
private DateTime _updatedDate;
}
public class User : BaseEntity
{
private string _username;
private string _password;
private Employee _employee;
//set get
}
私は、createdByとcreatedDateではなく、ユーザー名、パスワード、および従業員を比較したいと思います。これを行う方法はありますか?私はグーグルで検索しようとしましたが、私はこの
if (!propertyInfo.Name.Equals("CreatedDate") ||
!propertyInfo.Name.Equals("CreatedBy"))
{
}
のようなアプローチには非常に間違って何かがあるようですので、私は、それをハードコーディングすることができ、何も が見つかりませんでした。なぜ単にIComparableを実装しないのですか? – leppie