C# 使用反射比较类

C# 使用反射比较类,c#,generics,reflection,C#,Generics,Reflection,问题是,当我使用反射来迭代用户类时,我无法识别Employee类。我的代码是这样的 public class User { private string _username; private string _password; private Employee _employee; //set get } public class Employee { private int _id; private string _firstname; priv

问题是,当我使用反射来迭代用户类时,我无法识别Employee类。我的代码是这样的

public class User
{
   private string _username;
   private string _password;
   private Employee _employee;

   //set get 
}

public class Employee
{
   private int _id;
   private string _firstname;
   private string _lastname;

   //set get
}
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 
}
公共字符串比较(T newVal,T oldVal)
{
StringBuilder retVal=新建StringBuilder();
类型objectsType=typeof(T);
foreach(PropertyInfo PropertyInfo在objectsType.GetProperties中(
BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance)
{
//如果(?)-->如何将这里的propertyInfo标识为类,以便使用递归进行迭代
//{
//比较(propertyInfo.GetValue(oldVal,null),propertyInfo.GetValue(newVal,null));
//}
if(propertyInfo.CanRead)
{
object newValue=propertyInfo.GetValue(newVal,null);
对象oldValue=propertyInfo.GetValue(oldVal,null);
if(propertyInfo.PropertyType.Namespace.StartsWith(“系统”)和&!propertyInfo.PropertyType.IsGenericType)
{
如果(!Object.Equals(newValue,oldValue))
{
Append(propertyInfo.Name+“=”+newValue.ToString()+“;”);
}
}
}
}
返回retVal.ToString();
}
请帮忙, 多谢各位

问候,,
威利

你可以这样做:

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();
}

如何只选择一些属性可以比较。假设我修改了用户类

if(!propertyInfo.PropertyType.IsValueType && propertyInfo.PropertyType != typeof(string))
{
   //you're dealing with a class
}
我只想比较用户名、密码和员工,而不是createdBy和createdDate。有没有办法做到这一点?我试着用谷歌搜索,但什么也没找到 所以我只能硬编码,像这样

public class User
{
   private string _username;
   private string _password;
   private Employee _employee;

   //set get 
}

public class Employee
{
   private int _id;
   private string _firstname;
   private string _lastname;

   //set get
}
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 
}

这种方法似乎有很大的问题。为什么不直接实施
IComparable
?你应该为此开始一个新问题。但是您可以检查
DeclaringType
属性,以确定是否继承了成员。