Results 1 to 8 of 8

Thread: Compare class object propertiesd

  1. #1

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    Compare class object propertiesd

    Im trying to find a generic way to compare the settable property fields of an input class to
    the settable property fields on the output class when doing conversions. The classes may or may not be the same type but will be cast-able if they have all the same properties.

    Need to do a deep compare.

    Any ideas or suggestions?

    Thanks

    This is what track Im on so far but its not even close


    Code:
            [TestMethod]
            public void TestMethod1()
            {
                var contactData = new ContactData();
                var contact = contactData.AsContact();
    
                foreach (var item in contact.GetType().GetProperties())
                {
                    //if (!Object.Equals(item.GetValue(contact, null), contactData.GetType().GetProperty(item.Name).GetValue(contactData, null)))
                    //{
                        //diffProperties.Add(item);
                    //}
                    if (!InstanceEquals(contactData, contact))
                    {
                        diffProperties.Add(item);
                    }
                }
            }
    
    public static bool InstanceEquals(object self, object to, params string[] ignore)
            {
                if (self != null && to != null)
                {
                    Type type = self.GetType();
                    foreach (System.Reflection.PropertyInfo pi in type.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance))
                    {
                        if (!diffProperties.Contains(pi.Name))
                        {
                            object selfValue = type.GetProperty(pi.Name).GetValue(self, null);
                            object toValue = type.GetProperty(pi.Name).GetValue(to, null);
    
                            if (selfValue != toValue && (selfValue == null || !selfValue.Equals(toValue)))
                            {
                                return false;
                            }
                        }
                    }
                    return true;
                }
                return self == to;
            }
    Last edited by RobDog888; Nov 10th, 2014 at 05:57 PM.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  2. #2
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,660

    Re: Compare class object propertiesd

    Reflection seems like the right route,

    what is this -
    Code:
    PropertyInfo pi in type.GetProperties
    returning you?

    So your flags should return you;

    public items
    Code:
    System.Reflection.BindingFlags.Public
    and

    private items
    BindingFlags.NonPublic | BindingFlags.Instance
    Is that what you want? or do you just want the public items?

    Also do you just want properties or also methods and events?
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  3. #3

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    Re: Compare class object propertiesd

    Yea reflection works but you take a performance hit. Not a concern here since its a unit test only otherwise I'd use Linq.

    This is what I left off with last night.

    Code:
            {
                IList diffProperties = new ArrayList();
    
                foreach (MethodInfo mi in (typeof(InsuranceAdapter)).GetMethods())
                {
                    var contactData = new ContactData ();
                    var contact = contactData.AsContact();
    
                    foreach (var item in contact.GetType().GetProperties().Where(x => x.CanWrite))
                    {
                        if (contactData.GetType().GetProperty(item.Name) != null)
                        {
                            object selfValue = item.GetValue(contact, null);
                            object toValue = contactData.GetType().GetProperty(item.Name).GetValue(contactData, null);
                            if ((selfValue != null && toValue != null) && (selfValue.ToString().GetHashCode() != toValue.ToString().GetHashCode()))
                            {
                                diffProperties.Add(item);
                            }
                        }
                        else
                        {
                            diffProperties.Add(item);
                        }
                    }
                }
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    Re: Compare class object propertiesd

    Think this is just about it. Still need to figure out how to filter it down to just the user defined methods and not get the system methods like .ToString()

    Code:
            // Must be a static class to properly evaluate
            private bool compareConverterSettableProperties(Type classType)
            {
                IList diffProperties = new ArrayList();
    
                foreach (MethodInfo mi in (classType).GetMethods())
                {
                    var miDeclaringType = mi.GetParameters().First().ParameterType;
                    var miReturnType = mi.ReturnParameter.ParameterType;
    
                    var contactData = Activator.CreateInstance(miReturnType);
                    var contact = Activator.CreateInstance(miDeclaringType);
    
                    foreach (var item in contact.GetType().GetProperties().Where(x => x.CanWrite))
                    {
                        if (contactData.GetType().GetProperty(item.Name) != null)
                        {
                            object selfValue = item.GetValue(contact, null);
                            object toValue = contactData.GetType().GetProperty(item.Name).GetValue(contactData, null);
                            if ((selfValue != null && toValue != null) && (selfValue.ToString().GetHashCode() != toValue.ToString().GetHashCode()))
                            {
                                diffProperties.Add(item);
                            }
                        }
                        else
                        {
                            diffProperties.Add(item);
                        }
                    }
                }
    
                if (diffProperties.Count > 0)
                {
                    return false;
                }
                return true;
            }
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Compare class object propertiesd

    Quote Originally Posted by RobDog888 View Post
    Still need to figure out how to filter it down to just the user defined methods and not get the system methods like .ToString()
    You can determine whether a member is declared in the type you reflected or inherited by comparing the DeclaringType and ReflectedType properties of a MemberInfo. If they are different then the member is inherited.

    That's only going to distinguish the top-level type form everything else in the inheritance chain though. If you're looking to make a distinction somewhere else in that chain then that will require a bit more work.

  6. #6
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,660

    Re: Compare class object propertiesd

    You could also use the - BindingFlags.DeclaredOnly Flag to return only non-inherited types.
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Compare class object propertiesd

    Quote Originally Posted by NeedSomeAnswers View Post
    You could also use the - BindingFlags.DeclaredOnly Flag to return only non-inherited types.
    Much better.

  8. #8

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    Re: Compare class object propertiesd

    Thanks guys. I did come across declaring type but I need an in depth comparison. This is to be used in Unit Testing to validate that converters are completely and properly written so nothing is missed.

    An input extension class is passed in which is evaluated for its conversion methods. Then each conversion method will get inspected to compare the settable fields and ignore all the other system junk.

    Currently I just need to filter out the System junk
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width