简体   繁体   中英

how to get all property whith references from this object

This My architecture Models

    public abstract class BaseDocument
            {
                public long Id { get; set; }
            }
        public class Candidate : BaseDocument
            {
                public string Email { get; set; }
                public IList<CurrentAddress> CurrentAddressList { get; set; }
            }
        public class CurrentAddress : BaseDocument
            {
                public string Street { get; set; }
                public string City { get; set; }
                public Contact CandidateContact { get; set; }
            }
        public class Contact:BaseDocument
            {
                public string Name { get; set; }
                public string Address { get; set; }
                public IList<Telephone> Telephones { get; set; }
            }
        public class Telephone:BaseDocument
            {
                public string PhoneNumber { get; set; }
                public string PhoneTypeCode { get; set; }
            }

This code not working for me when I wont get all property value

        Candidate candidate = new Candidate(); 
        var allPropertyValue = GetAllPropertiesValue(candidate); //object candidate has data!!!!
    
        public List<EntityPropertyValue> entityPropertiesValue;
        public List<EntityProperty> GetAllPropertiesValue(object entity)  
                {
                    this.entityProperties = new List<EntityProperty>();
                    var currentType = entity.GetType();
                    foreach (var propertyInfo in currentType.GetProperties())
                    {
                        var value = this.GetPropertyValue(entity, propertyInfo.Name);
                        this.entityProperties.Add(new EntityProperty(propertyInfo.PropertyType, propertyInfo.Name, value));
                    }
                    return this.entityProperties;
                }
        
        public class EntityProperty
            {
                public Type PropertyType { get; set; }
                public string PropertyName { get; set; }
                public dynamic PropertyValue { get; set; }
        
                public EntityProperty(Type propertyType, string propertyName, dynamic proppertyValue)
                {
                    PropertyType = propertyType;
                    PropertyName = propertyName;
                    PropertyValue = proppertyValue;
                    
                }
            }
        public static object GetPropValue(object src, string propName)
         {
             return src.GetType().GetProperty(propName).GetValue(src, null);
         }

its working like this:

Candidate candidate = new Candidate(); 
var allPropertyValue = GetAllPropertiesValue(candidate); //object candidate has data!!!!
    
 List<EntityProperty> entityProperties;
 List<EntityProperty> GetAllPropertiesValue(object entity)  
{
    entityProperties = new List<EntityProperty>();
    var currentType = entity.GetType();
    foreach (var propertyInfo in currentType.GetProperties())
    {
        var value = GetPropValue(entity, propertyInfo.Name);
        entityProperties.Add(new EntityProperty(propertyInfo.PropertyType, propertyInfo.Name, value));
    }
    return entityProperties;
}
 static object GetPropValue(object src, string propName)
{
    return src.GetType().GetProperty(propName).GetValue(src, null);
} 

debug view

is it your expectation?

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