lundi 12 février 2018

C# - How to get properties of class member which is also a class?

I have the following extension method which fills in missing values of a class member from a source object of the same class. The missing part of this method is when a member of the class is also a class object of another class that also has members of its own, so I need a recursion here, but I didn't find any way to get the inner members of the class member and being able to pass it back to the FillValues method...

For example, I have a class called User and a class called UserLogin like this:

public class User
{
    public string FirstName;
    public string LastName;
    public UserLogin Login;
}

public class UserLogin
{
    public string Email;

    public string Password;
}

When I call FillValues on a member of class User if fills in the missing fields from FirstName and LastName but not from Login because its a class member also.

How can I pass the member Login recursively so that nested members will also be filled with values?

public static void FillValues<T>(this T target, T source)
{
    FillMissingProperties(target, source);
    FillMissingFields(target, source);
}

private static void FillMissingProperties<T>(T target, T source)
{
    var properties = typeof(T).GetProperties().Where(prop => prop.CanRead && prop.CanWrite);
    foreach (var prop in properties)
    {
        var targetValue = prop.GetValue(target, null);
        var defaultValue = prop.PropertyType.GetTypeInfo().IsValueType ? Activator.CreateInstance(prop.PropertyType) : null;

        if (targetValue == null || targetValue.Equals(defaultValue))
        {
            var sourceValue = prop.GetValue(source, null);
            prop.SetValue(target, sourceValue, null);
        }
    }
}

private static void FillMissingFields<T>(T target, T source)
{
    var fields = typeof(T).GetFields();
    foreach (var field in fields)
    {
        var targetValue = field.GetValue(target);
        var sourceValue = field.GetValue(source);
        var defaultValue = field.FieldType.GetTypeInfo().IsValueType ? Activator.CreateInstance(field.FieldType) : null;

        if (targetValue == null || targetValue.Equals(defaultValue))
            field.SetValue(target, sourceValue);
    }
}





Aucun commentaire:

Enregistrer un commentaire