Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在属性级别使用通用比较方法扩展EF实体 public static bool PropertiesEqual(T self,T to,params string[]skip) T:在哪里上课 { if(self!=null&&to!=null) { var selfType=self.GetType(); var skipList=新列表(跳过); 在selfType.GetProperties(BindingFlags.Public)中的foreach(PropertyInfo)pi| BindingFlags.Instance) { 如果(skipList.Contains(pi.Name))继续; var selfValue=selfType.GetProperty(pi.Name).GetValue(self,null); var-toValue=selfType.GetProperty(pi.Name).GetValue(to,null); 如果(selfValue!=toValue&&(selfValue==null|| !selfValue.Equals(toValue))) { 返回false; } } 返回true; } 返回self==到; }_C#_Asp.net_Entity Framework - Fatal编程技术网

C# 在属性级别使用通用比较方法扩展EF实体 public static bool PropertiesEqual(T self,T to,params string[]skip) T:在哪里上课 { if(self!=null&&to!=null) { var selfType=self.GetType(); var skipList=新列表(跳过); 在selfType.GetProperties(BindingFlags.Public)中的foreach(PropertyInfo)pi| BindingFlags.Instance) { 如果(skipList.Contains(pi.Name))继续; var selfValue=selfType.GetProperty(pi.Name).GetValue(self,null); var-toValue=selfType.GetProperty(pi.Name).GetValue(to,null); 如果(selfValue!=toValue&&(selfValue==null|| !selfValue.Equals(toValue))) { 返回false; } } 返回true; } 返回self==到; }

C# 在属性级别使用通用比较方法扩展EF实体 public static bool PropertiesEqual(T self,T to,params string[]skip) T:在哪里上课 { if(self!=null&&to!=null) { var selfType=self.GetType(); var skipList=新列表(跳过); 在selfType.GetProperties(BindingFlags.Public)中的foreach(PropertyInfo)pi| BindingFlags.Instance) { 如果(skipList.Contains(pi.Name))继续; var selfValue=selfType.GetProperty(pi.Name).GetValue(self,null); var-toValue=selfType.GetProperty(pi.Name).GetValue(to,null); 如果(selfValue!=toValue&&(selfValue==null|| !selfValue.Equals(toValue))) { 返回false; } } 返回true; } 返回self==到; },c#,asp.net,entity-framework,C#,Asp.net,Entity Framework,我想用一种扩展方法来扩展我的EF实体,该方法比较两个实例的基本属性(?)(属性,如数字、字符串、布尔和非附加对象) 我想知道的是,有没有可能把它作为一种扩展方法?或者我需要在POCO类中为我想要执行的每个EF类型定义它吗instance1.PropertiesEqual(instance2) 我想知道的第二件事是如何正确地只针对我上面提到的数据类型,并跳过附加的对象(连接的表)。要回答第一个问题,您可以轻松地将其定义为一个扩展方法,只要该方法存在于静态类中 其次,如果您检查,您将只获得strin

我想用一种扩展方法来扩展我的EF实体,该方法比较两个实例的基本属性(?)(属性,如数字、字符串、布尔和非附加对象)

我想知道的是,有没有可能把它作为一种扩展方法?或者我需要在POCO类中为我想要执行的每个EF类型定义它吗
instance1.PropertiesEqual(instance2)


我想知道的第二件事是如何正确地只针对我上面提到的数据类型,并跳过附加的对象(连接的表)。

要回答第一个问题,您可以轻松地将其定义为一个扩展方法,只要该方法存在于静态类中

其次,如果您检查,您将只获得string和struct属性,这两个属性都有Object.Equals的默认实现

此外,此实现将在执行(昂贵的)逐属性比较之前检查null和相等性

public static bool PropertiesEqual<T>(T self, T to, params string[] skip) 
where T : class
{
    if (self != null && to != null)
    {
        var selfType = self.GetType();

        var skipList = new List<string>(skip);
        foreach (PropertyInfo pi in selfType.GetProperties(BindingFlags.Public |
                                                         BindingFlags.Instance))
        {
            if (skipList.Contains(pi.Name)) continue;

            var selfValue = selfType.GetProperty(pi.Name).GetValue(self, null);
            var toValue = selfType.GetProperty(pi.Name).GetValue(to, null);

            if (selfValue != toValue && (selfValue == null ||
                                         !selfValue.Equals(toValue)))
            {
                return false;
            }
        }
            return true;
    }
    return self == to;
}
如果希望加快速度,可以使用动态方法进行比较

public static bool PropertiesEqual<T>(T self, T to, params string[] skip) 
where T : class
{
    if (self != null && to != null)
    {
        var selfType = self.GetType();

        var skipList = new List<string>(skip);
        foreach (PropertyInfo pi in selfType.GetProperties(BindingFlags.Public |
                                                         BindingFlags.Instance))
        {
            if (skipList.Contains(pi.Name)) continue;

            var selfValue = selfType.GetProperty(pi.Name).GetValue(self, null);
            var toValue = selfType.GetProperty(pi.Name).GetValue(to, null);

            if (selfValue != toValue && (selfValue == null ||
                                         !selfValue.Equals(toValue)))
            {
                return false;
            }
        }
            return true;
    }
    return self == to;
}
publicstaticboolpropertiesequal(此T self,T other,参数字符串[]跳过)
{
if(self==null)返回other==null;
如果(self.Equals(other))返回true;
var properties=来自typeof(T).GetProperties()中的p
其中!skip.Contains(p.Name)
&&!p.PropertyType.IsByRef//只接受结构和字符串
选择p;
foreach(属性中的var p)
{
var selfValue=p.GetValue(self);
var otherValue=p.GetValue(其他);
如果(!object.Equals(selfValue,otherValue))
返回false;
}
返回true;
}

基元类型不是
(引用类型)。谢谢,“结构属性”包括布尔和数字?是的,包括所有基元,如int、bool、DateTime、long、double、float等
GetValue(self)
应该是
GetValue(self,null)
对吗?使用.NET 4.5您不必这样做,在旧版本中您可能会这样做。忘记提到这是4.0,所以是的,我这样做了。再次感谢