Как получить стоимость недвижимости - PullRequest
0 голосов
/ 25 мая 2018

Здравствуйте, люди, у меня следующая проблема:

public class Document
{
    public Header Header {get;set;}
    public Footer Footer{get;set;}
    public string Text{get;set;}
    public string Description{get;set;}
    public int NumberOfPages{get;set;}
}
public class Header
{
    public int Id{get;set;}
    public string Text{get;set;}
}
public class Footer
{
    public int Id{get;set;}
    public string Text{get;set;}
}

Представьте, что у меня есть этот домен, я хотел бы скопировать все примитивные свойства Document и те, которые не являются примитивными, такие какЗаголовок и нижний колонтитул Я хотел бы просто текст.

У меня есть следующий код, чтобы просто скопировать примитивные свойства:

public static List<DataPropertyReport> GetPrimitiveProperties<T>(T entity)
{
    var properties = entity.GetType().GetProperties();    
    List<DataPropertyReport> info = new List<DataPropertyReport>();

    foreach (var property in properties)
    {
        Object value = property.GetValue(entity, null);
        Type type = value != null ? value.GetType() : null;

        if (type != null && 
               (type.IsPrimitive || 
                type == typeof(string) || 
                type.Name == "DateTime"))
        {
            var name = property.Name;
            info.Add(new DataPropertyReport(name, value.ToString(), 1));
        }
    }    
    return info;
}

1 Ответ

0 голосов
/ 25 мая 2018

Вы можете переопределить ToString() для непримитивных типов и просто вызвать эту перегрузку:

public class Header
{
    public int Id { get; set; }
    public string Text { get; set; }

    public override string ToString()
    {
        return Text;
    }
}
public class Footer
{
    public int Id { get; set; }
    public string Text { get; set; }

    public override string ToString()
    {
        return Text;
    }
}
...