Object does not match target type PropertyInfo SetValue - one class to another

1k views Asked by At

So I have 2 classes, both have identical Property names. One class contains different variables: int, strings, bool and DateTime The second class contains only 1 int and the rest are all strings.

Now I want to loop through all the properties, get the value from class1, encrypt that data and save it as a string in obj2, then return it to the main form (to save it in a database later).

public PersoonEncrypted EncryptPersonClass(Class1 object1)
    {
        PersoonEncrypted persEncrypt = new PersoonEncrypted(); //second class obj

        Type type = object1.GetType();
        PropertyInfo[] properties = type.GetProperties();
        Type type2 = persEncrypt.GetType();
        PropertyInfo[] properties2 = type.GetProperties();
        foreach (var bothProperties in properties.Zip(properties2, (obj1, obj2) => new { Obj1 = obj1, Obj2 = obj2 }))
        {
            string value = "";

            value = bothProperties.Obj1.GetValue(object1) as string;
            if (!string.IsNullOrWhiteSpace(value))
            {
                string encryptValue = Encrypt(value);
                if ((bothProperties.Obj2 != null) && (bothProperties.Obj2.PropertyType == typeof(string)))
                { //!= null check has no effect at all
                    bothProperties.Obj2.SetValue(persEncrypt, encryptValue, null); //errorLine
                }
            }

        }

        return persEncrypt;
    }

That is what I came up with until now. I have, of course, searched for other solutions like this one. This, after applying some own changes, didn't return any errors, but it didn't save any encrypted strings into the class persEncrypt. What I concluded was, from that test, is that it was testing if the value in the second class(persEncrypt in my example) from the particular property was null, while it shouldn't do that, it should make a new instance of that variable and save it in the object class, but removing that check gave me the same error.

1

There are 1 answers

1
David On
  • you're just .Zip-ing the two lists of PropertyInfo objects, which simply iterates through both lists and doesn't check or sort for any sort of matching. This could result in erroneous behavior depending on the order in which properties appear - consider using a .Join instead to match property names.
  • This code doesn't check for an indexer on the property before attempting to assign to it without one - any indexed property which is of type string will make it to this point and then throw an exception when you try to set it.
  • Because this code is calling into Properties, there's the possibility an exception is being thrown by the code of the Property itself. This is where a StackTrace from your exception could reveal much more about what's happening.
  • Your code also checks for a property of type string directly - when using reflection you should use IsAssignableFrom instead in order to allow for inherited types, though that is unlikely the issue in this one case.