While we appreciate comments from our users, please follow our posting guidelines. Have you tried the Cyotek Forums for support from Cyotek and the community?

Styling with Markdown is supported

Original Comment

Suri

# Reply

I just added a few more simple lines of code for my requirement at the beginning to do some extra checks to avoid the loop:

bool result;

        if (objectActual != null && objectModified != null)
        {
            Type objectTypeActual;
            Type objectTypeModified;
            result = true; // assume by default they are equal

            objectTypeActual = objectActual.GetType();    
            objectTypeModified = objectModified.GetType();  

            if(objectTypeActual.FullName !=  objectTypeModified.FullName)
            {
                Console.WriteLine("Objects are of different type. Cannot Compare '{0}' & '{1}'.", objectTypeActual.FullName, objectTypeActual.FullName);
                result = false;
                return result;
            }

            var allPropertyInfos = objectTypeActual.GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(p => p.CanRead && !ignoreList.Any(x => x.Contains(p.Name)));
            var allPropertyInfosModified = objectTypeModified.GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(p => p.CanRead && !ignoreList.Any(x => x.Contains(p.Name)));

            if (allPropertyInfos.Count == 0 && allPropertyInfosModified.Count == 0)
            {
                Console.WriteLine("There are no properties in these objects to compare. These are both equal.);
                result = true;
                return result;
            }
            if (allPropertyInfos.Count <= 0)
            {
                Console.WriteLine("Object of type '{0}' does not have any properties to Compare.", objectTypeActual.FullName);
                result = false;
                return result;
            }

            if (allPropertyInfosModified.Count <= 0)
            {
                Console.WriteLine("Object of type '{0}' does not have any properties to Compare.", objectTypeModified.FullName);
                result = false;
                return result;
            }

            foreach (PropertyInfo propertyInfo in allPropertyInfos)

{ .....