簡體   English   中英

C# - 如何獲取類成員的屬性,這也是一個類?

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

我有以下擴展方法,它從同一個類的源對象填充類成員的缺失值。 這個方法缺少的部分是當一個類的成員也是另一個類的類對象時,它也有自己的成員,所以我需要一個遞歸,但我沒有找到任何方法來獲得內部成員類成員並能夠將其傳遞回FillValues方法...

例如,我有一個名為User的類和一個名為UserLogin的類,如下所示:

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

public class UserLogin
{
    public string Email;

    public string Password;
}

當我在類User的成員上調用FillValues ,如果填寫了FirstNameLastName缺少的字段,而不是來自Login因為它也是類成員。

如何遞歸傳遞成員Login以便嵌套成員也將填充值?

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

您需要以遞歸FillValues為類類型的類字段/屬性調用FillValues 為此,您需要具有此方法的非泛型版本:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM