Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.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# 计算对象的变更集_C#_Generics_Windows Store Apps_Generic List_Generic Programming - Fatal编程技术网

C# 计算对象的变更集

C# 计算对象的变更集,c#,generics,windows-store-apps,generic-list,generic-programming,C#,Generics,Windows Store Apps,Generic List,Generic Programming,我正在编写一个windows应用商店应用程序,它从服务器接收一个对象(我创建的自定义类型),允许用户编辑该对象,然后将“变更集”提交回服务器。此变更集只是一个与接收对象类型相同的对象,除用户编辑的字段外,每个字段都设置为null。然后,这些字段包含用户的编辑,如下所示: Original Case: Description: "Cracked exhaust pipe" Status: "Open" Customer: "" Mileage: 10000 Edi

我正在编写一个windows应用商店应用程序,它从服务器接收一个对象(我创建的自定义类型),允许用户编辑该对象,然后将“变更集”提交回服务器。此变更集只是一个与接收对象类型相同的对象,除用户编辑的字段外,每个字段都设置为null。然后,这些字段包含用户的编辑,如下所示:

Original Case: Description: "Cracked exhaust pipe" Status: "Open" Customer: "" Mileage: 10000 Edited Case: Description: "" Status "Open" Customer: "Example inc." Mileage: 10000 Case changeset: Description: "" Status: null Customer: "Example inc." Mileage: null 该方法适用于我遇到的每个属性(列表除外),因此我创建了if子句来处理列表。由于
list1.Equals(list2)
不比较列表中的项目,我们需要将
oldValue
newValue
转换为
List
才能使用
List.SequenceEqual()

当我尝试使用类型或命名空间名称“listType”创建新列表时,为什么会出现这样的错误:找不到类型或命名空间名称“listType”(是否缺少using指令或程序集引用??如果有更好的方法解决这个问题,我愿意接受建议。

试试这个:

IList oldList = (IList)oldValue; //Visual studio complains about
IList newList = (IList)newValue; //listType not being found

if (!NonGenericSequenceEqual(oldList, newList))
{
    changeSet.GetType().GetRuntimeProperty(property.Name).SetValue(changeSet, newValue, null);
}

public static bool NonGenericSequenceEqual(IList a, IList b)
{
    if (ReferenceEquals(a, b))
    {
        return true;
    }
    else if (a == null || b == null)
    {
        return false;
    }
    else
    {
        int count = a.Count;

        if (count != b.Count)
        {
            return false;
        }
        else
        {
            for (int i = 0; i < count; i++)
            {
                if (!Object.Equals(a[i], b[i]))
                {
                    return false;
                }
            }

            return true;
        }
    }
}

不仅如此,实现
IList
的类也不一定实现
IList
。尽管可以通过使用
IEnumerable
而不是
IList
来克服这一问题,但反射解决方案不起作用。。windows应用商店框架中不存在GetMethod,但GetRuntimeMethod存在。然而,GetRuntimeMethod需要一个类型数组作为第二个参数,因此我们回到了原始问题(无法使用listType)。然而,我最终使用了一个稍微修改过的NonGenericSequenceEqual版本,因此非常感谢,我仍然很好奇为什么visual studio找不到listType,因为它是在上一个版本中声明的line@jbb
listType
是一个在运行时保存类型的变量。要以您尝试使用
oldList
newList
的方式声明泛型类型的变量,必须指定编译时已知的类型。编译器是正确的:
listType
不是类型的名称-它是在运行时引用类型的变量的名称-从语法上讲,只有类型名称或类型参数在
之间有效。
IList oldList = (IList)oldValue; //Visual studio complains about
IList newList = (IList)newValue; //listType not being found

if (!NonGenericSequenceEqual(oldList, newList))
{
    changeSet.GetType().GetRuntimeProperty(property.Name).SetValue(changeSet, newValue, null);
}

public static bool NonGenericSequenceEqual(IList a, IList b)
{
    if (ReferenceEquals(a, b))
    {
        return true;
    }
    else if (a == null || b == null)
    {
        return false;
    }
    else
    {
        int count = a.Count;

        if (count != b.Count)
        {
            return false;
        }
        else
        {
            for (int i = 0; i < count; i++)
            {
                if (!Object.Equals(a[i], b[i]))
                {
                    return false;
                }
            }

            return true;
        }
    }
}
if (!(bool)typeof(System.Linq.Enumerable).GetMethod("SequenceEqual").MakeGenericMethod(oldValue.GetType().GetRuntimeProperty("Item").PropertyType).Invoke(null, new object[] { oldObject, newObject }))
{
    changeSet.GetType().GetRuntimeProperty(property.Name).SetValue(changeSet, newValue, null);
}