lundi 19 février 2018

c# Finding and updating object based on a type using reflection

I’m trying to check the values in a nested object and replace them if necessary. Using reflection I can iterate through all the nested objects and get the property types but I’m stuck retrieving the values and replacing the values. I threw together a simple example.

Say I have the following model:

public class Employee
{
    #region Members
    private string _FirstName;
    private string _LastName;
    private StreetAddress _EmployeeAddress;
    #endregion
    #region Properties
    public string FirstName { get { return _FirstName; } set { value = _FirstName; } }
    public string LastName { get { return _LastName; } set { value = _LastName; } }
    public StreetAddress EmployeeAddress { get { return _EmployeeAddress; } set { value = _EmployeeAddress; } }
    #endregion
}

public class StreetAddress
{
    #region Members
    private string _Street;
    private string _City;
    private string _State;
    private string _Zip;
    #endregion
    #region Properties
    public string Street { get { return _Street; } set { value = _Street; } }
    public string City { get { return _City; } set { value = _City; } }
    public string State { get { return _State; } set { value = _State; } }
    public string Zip { get { return _Zip; } set { value = _Zip; } }
    #endregion
}

In my scenario I need to update the Street in StreetAddress from “123 Main Street” to “999 Main Street” for any object that StreetAddress appears in. The StreetAddress object could appear in any level of any give object. So let’s say I have Employee cast as an object and I’m using the following to iterate through the Employee object looking for the StreetAddress type.

private static void ReadPropertiesRecursive2(Type type)
        {
            foreach (PropertyInfo property in type.GetProperties())
            {

                if (property.PropertyType == typeof(StreetAddress))
                {
                    Debug.WriteLine(property.Name);

                    Type StreetAddressType = property.GetType();

                    //Get the value of 'Street'  
                    //Here is where I'm having trouble figuring out how to get the value

                    Debug.WriteLine(StreetAddressType.GetProperty("Street").GetValue(StreetAddressType, null));
                    //The above throws the Exception: 'System.Reflection.TargetException'

                    //Check the value of 'Street'

                    //Replace the value of 'Street'

                }
                if (property.PropertyType.IsClass)
                {
                    ReadPropertiesRecursive2(property.PropertyType);
                }
            }
        }

Usage:

ReadPropertiesRecursive2(Employee.GetType());

I'm not sure what I'm missing in the syntax to use the 1) return the value of and 2) update the value if necessary. If you could point me in right direction, that would be awesome or if there is a better method I'd be interested to know about it. Thanks in advance!





Aucun commentaire:

Enregistrer un commentaire