繁体   English   中英

c#反射比较具有布尔属性的2个对象

[英]c# reflection comparing 2 objects with boolean properties

我有两个具有布尔类型属性的两个对象。 属性在命名方面是相同的,我只是想确保它们相等。 我做了很多这样的检查,想知道是否有人对一种方法有任何建议,该方法可以在一个步骤中完成以下代码,但如果它们具有相同的名称,它是否足够动态以按名称检查属性选项?

 if (options != null && other != null)
            return options.Quantities == other.Quantities &&
                   options.SKUEntityKey == other.SKUEntityKey &&
                   options.LineItemType_Type == other.LineItemType_Type &&
                   options.SKUIdentifier == other.SKUIdentifier &&
                   options.Identifier == other.Identifier;

如果我问的不清楚,请告诉我

   class Program
    {
        static void Main(string[] args)
        {
            ReflectOverProperties<TestClass, TestClass2>(new TestClass(), new TestClass2());
            Console.ReadLine();
        }

        public static void ReflectOverProperties<T, Z>(T x, Z y)
        {
            var properties = typeof(T).GetProperties();
            foreach (var item in properties)
            {
                CompareProperty(x, y, item.Name);

            }
        }

        private static void CompareProperty<T, Z>(T x, Z y, string itemName)
        {
            dynamic originalValue = GetPropValue(x, itemName);
            dynamic newValue = GetPropValue(y, itemName);
            PropertyCompare(itemName, originalValue, newValue);

        }

        private static void PropertyCompare(string itemName, dynamic originalValue, dynamic newValue)
        {
            if (originalValue != newValue)
            {
             Console.Write($"Property {itemName} does not match");
            }
        }



        public static object GetPropValue(object src, string propName)
        {
            return src.GetType().GetProperty(propName).GetValue(src, null);
        }
    }

    public class TestClass
    {
        public TestClass()
        {
            Test1 = false;
            Test2 = false;
            Test3 = false;
        }
        public bool Test1 { get; set; }
        public bool Test2 { get; set; }
        public bool Test3 { get; set; }
    }
    public class TestClass2
    {
        public TestClass2()
        {
            Test1 = false;
            Test2 = false;
            Test3 = true;
        }
        public bool Test1 { get; set; }
        public bool Test2 { get; set; }
        public bool Test3 { get; set; }
    }

这是我修改得非常快的一些代码,它们在控制台应用程序中运行。 希望让你朝着正确的方向前进,现在做同样的事情可能会有所不同。 只需使用一些基本的反射和动态属性。

我希望这个糟糕的控制台应用程序至少能让你知道如何去做你想做的事:

    static void Main(string[] args)
    {
        Person steve = new Person()
        {
            IsHungry = true,
            IsLazy = false,
            IsSleepy = true
        };

        Dog bella= new Dog()
        {
            IsHungry = true,
            IsLazy = false,
            IsSleepy = true
        };

        bool match = DoAllBoolPropertiesMatch(steve, bella);

        Console.WriteLine($"\r\n----> Do Bools in Objects Match?: {match}");
    }

    private static bool DoAllBoolPropertiesMatch(object obj1, object obj2)
    {
        // For each Boolean property of object 1, check object 2:
        foreach(PropertyInfo propInfo in obj1.GetType().GetProperties())
        {
            // Property is boolean.
            if(propInfo.PropertyType == typeof(Boolean))
            {
                // Look for a property on obj2 with the same name that also returns a bool.
                PropertyInfo matchingPropInfo = obj2.GetType().GetProperty(propInfo.Name, typeof(Boolean));

                if(matchingPropInfo != null)
                {
                    Console.WriteLine($"Evaluating Property {propInfo.Name} from obj1:");
                    Console.WriteLine($"  - Value for Obj1 = [{propInfo.GetValue(obj1)}]");
                    Console.WriteLine($"  - Value for Obj2 = [{matchingPropInfo.GetValue(obj2)}]");

                    if(Convert.ToBoolean(propInfo.GetValue(obj1)) != Convert.ToBoolean(matchingPropInfo.GetValue(obj2)))
                        return false;
                }
            }
        }

        return true;
    }

    public class Person
    {
        public bool IsHungry { get; set; }
        public bool IsSleepy { get; set; }
        public bool IsLazy { get; set; }
    }

    public class Dog
    {
        public bool IsHungry { get; set; }
        public bool IsSleepy { get; set; }
        public bool IsLazy { get; set; }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM