相关疑难解决方法(0)

查找两个C#对象之间的属性差异

我正在处理的项目需要一些简单的审计日志记录,以便用户更改其电子邮件,帐单地址等.我们正在使用的对象来自不同的来源,一个是WCF服务,另一个是Web服务.

我已经使用反射实现了以下方法来查找对两个不同对象的属性的更改.这将生成一个属性列表,这些属性与旧值和新值有差异.

public static IList GenerateAuditLogMessages(T originalObject, T changedObject)
{
    IList list = new List();
    string className = string.Concat("[", originalObject.GetType().Name, "] ");

    foreach (PropertyInfo property in originalObject.GetType().GetProperties())
    {
        Type comparable =
            property.PropertyType.GetInterface("System.IComparable");

        if (comparable != null)
        {
            string originalPropertyValue =
                property.GetValue(originalObject, null) as string;
            string newPropertyValue =
                property.GetValue(changedObject, null) as string;

            if (originalPropertyValue != newPropertyValue)
            {
                list.Add(string.Concat(className, property.Name,
                    " changed from '", originalPropertyValue,
                    "' to '", newPropertyValue, "'"));
            }
        }
    }

    return list;
}
Run Code Online (Sandbox Code Playgroud)

我正在寻找System.IComparable,因为"所有数字类型(如Int32和Double)都实现IComparable,String,Char和DateTime也是如此." 这似乎是找到任何不是自定义类的属性的最佳方法.

利用由WCF或Web服务代理代码生成的PropertyChanged事件听起来不错,但没有为我的审计日志(旧值和新值)提供足够的信息.

如果有更好的方法来寻找输入,谢谢!

@Aaronaught,这里是一些基于执行object.Equals生成正匹配的示例代码:

Address …
Run Code Online (Sandbox Code Playgroud)

.net c# reflection auditing

55
推荐指数
3
解决办法
3万
查看次数

这个反射代码出了什么问题?GetFields()返回一个空数组

C#,Net 2.0

这是代码(我拿出了所有特定于域的东西,它仍然返回一个空数组):

using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            ChildClass cc = new ChildClass();
            cc.OtherProperty = 1;

            FieldInfo[] fi = cc.GetType().GetFields();
            Console.WriteLine(fi.Length);
            Console.ReadLine();
        }
    }
    class BaseClass<T>
    {
        private int myVar;

        public int MyProperty
        {
            get { return myVar; }
            set { myVar = value; }
        }


    }

    class ChildClass : BaseClass<ChildClass>
    {
        private int myVar;

        public int OtherProperty
        {
            get { return myVar; }
            set { …
Run Code Online (Sandbox Code Playgroud)

c# reflection type-parameter

23
推荐指数
3
解决办法
2万
查看次数

获取 2 个列表之间的差异

我有两个列表(ListAListB),这些列表的类型相同PersonInfoLogin字段是唯一键。

public class PersonInfo
{
    public string Login { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
    public bool Active { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我想比较这两个列表:

  1. 我想得到一个ListA不可用的项目列表ListB

  2. 对于两个列表中可用的项目,我想从ListALogin字段是唯一键)获取两个列表之间存在差异的项目的列表。

示例:如果对于中的Login“MyLogin” ListA, 的值FirstName与 中的值不匹配ListB。带有“MyLogin”的项目Login必须是结果列表的一部分。

示例:如果特定登录Age之间的ListAListB不同,则该项目必须是结果的一部分

谢谢。

.net c# linq

3
推荐指数
1
解决办法
2万
查看次数

标签 统计

c# ×3

.net ×2

reflection ×2

auditing ×1

linq ×1

type-parameter ×1