简体   繁体   中英

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);
    }
}

You need to recursively call FillValues for the class fields/properties that are of a class type. To do this you need to have a non generic version of this method:

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

private static void FillValues(Type type, object target, object source)
{
    FillMissingProperties(type, target, source);
    FillMissingFields(type, target, source);
}
private static void FillMissingProperties(Type type, object target, object source)
{
    var properties = type.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);
        }
        else if (targetValue != null && prop.PropertyType != typeof(string) && prop.PropertyType.GetTypeInfo().IsClass)
        {
            var sourceValue = prop.GetValue(source, null);
            FillValues(prop.PropertyType, targetValue, sourceValue);
        }
    }
}

private static void FillMissingFields(Type type, object target, object source)
{
    var fields = type.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);
        }
        else if(targetValue != null && field.FieldType  != typeof(string) && field.FieldType.GetTypeInfo().IsClass)
        {
            FillValues(field.FieldType, targetValue, sourceValue);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM