让我们有一个带有 int 属性的简单类
public class SimpleClass {
public int myInt { get; set; }// for having a property and not "public int myInt;", see Jon Skeet remark
}
我实例化它两次分配 myInt 与否
assignedObject = new SimpleClass() { myInt=0};
notAssignedObject = new SimpleClass();
现在通过反思,我在每种情况下使用查询 myInt 的值
Object value;
value=assignedObject.GetType().GetProperties().Where(o=>o.Name.Equals("myInt")).First().GetValue(assignedObject,null)
value=notAssignedObject.GetType().GetProperties().Where(o=>o.Name.Equals("myInt")).First().GetValue(notAssignedObject,null)
对于 myInt,我得到了两倍的 0,但我需要能够区分它们。如何?