简体   繁体   English

如何获得财产的价值

[英]How to get a value of a property

Hello people i'm with the next problem: 您好,我遇到下一个问题:

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

Imagine i have this domain, i will like to copy all the primitive properties of Document and the ones that aren't primitive, such as Header and Footer i would like just the text. 想象一下我拥有这个域,我想复制Document的所有原始属性以及非原始属性,例如Header和Footer,我只需要文本。

I have the next code just to copy the properties which are primitive: 我有下一个代码只是要复制原始属性:

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

You could override ToString() on the non-primitive types and just call that overload: 您可以在非原始类型上重写 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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获得财产的价值? - How to get value of a property? 如何从另一个房产获得房产价值? - How to get property value from another property? 如何获得财产名称和价值 - How to get the Property Name and Value 如何从ActionFilter中的属性获取值 - How to get a value from a property in actionfilter 如何获得自定义属性的值? - How can i get the value of the custom property? 如何从基类获取属性值? - how to get value of property from the base class? 如何通过属性值从字典中获取项目 - How to get item from dictionary by value of property 如何从 IEnumerable 获取属性值<object><div id="text_translate"><p>我有一个返回 IEnumerable 的方法</p><pre>public static IEnumerable&lt;object&gt; GetProps&lt;T&gt;(T obj) { var result = obj.GetType().GetProperties().Select(x =&gt; new { property = x.Name, value = x.GetValue(obj) }).Where(x =&gt; x.value == null).ToList(); return result; }</pre><p> 上面的代码将返回结果为[{"property":"YearOfBirth","value":null}]</p><p> 我现在尝试从返回的结果中获取属性值<strong>YearOfBirth</strong> 。 有人可以建议/帮助吗?</p></div></object> - How to get property value from IEnumerable<object> 如何从 OnActionExecuting 中的 model 获取属性值 - How to get a property value from model in OnActionExecuting 如何使用字符串变量获取属性值? - How to get the Property value using string Variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM