首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >List<PropertyInfo> ( List<PropertyInfo>不工作除外)

List<PropertyInfo> ( List<PropertyInfo>不工作除外)
EN

Stack Overflow用户
提问于 2017-03-01 14:51:35
回答 4查看 578关注 0票数 1

我正在创建一个帮助方法,它将自动为给定实体(类)的属性设置随机值,以便在测试时不必用值填充每个属性。

在我的例子中,每个实体继承自BaseEntity类,该类具有ID、CreatedBy、CreatedOn等.属性。基本上,这个类具有在所有实体之间共享的所有属性。

我在这里要做的是把独特的属性和普通的属性分开。

这是我的密码:

代码语言:javascript
复制
public static TEntity PopulateProperties<TEntity>(TEntity entity)
{
    try
    {
        // Since every entity inherits from EntityBase, there is no need to populate properties that are in EntityBase class
        // because the Core project populates them.
        // First of all, we need to get all the properties of EntityBase
        // and then exlude them from the list of properties we will automatically populate

        // Get all properties of EntityBase
        EntityBase entityBase = new EntityBase();
        List<PropertyInfo> entityBaseProperties = new List<PropertyInfo>();
        foreach (var property in entityBase.GetType().GetProperties())
        {
            entityBaseProperties.Add(property);
        }

        // Get all properties of our entity
        List<PropertyInfo> ourEntityProperties = new List<PropertyInfo>();
        foreach (var property in entity.GetType().GetProperties())
        {
            ourEntityProperties.Add(property);
        }

        // Get only the properties related to our entity
        var propertiesToPopulate = ourEntityProperties.Except(entityBaseProperties).ToList();

        // Now we can loop throught the properties and set values to each property
        foreach (var property in propertiesToPopulate)
        {
            // Switch statement can't be used in this case, so we will use the if clause                  
            if (property.PropertyType == typeof(string))
            {
                property.SetValue(entity, "GeneratedString");
            }
            else if (property.PropertyType == typeof(int))
            {
                property.SetValue(entity, 1994);
            }
        }

        return entity;
    }
    finally
    {
    }
} 

问题在于var propertiesToPopulate = entityBaseProperties.Except(ourEntityProperties).ToList();

我期望的是一个只对这个实体是唯一的PropertyInfo对象的列表,但是我总是获取实体的所有属性。此行不按预期对列表进行筛选。

有什么帮助为什么??

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2017-03-01 15:12:16

另一种可能是在for循环中签入DeclaringType是否与entity的类型相同。

代码语言:javascript
复制
// Get all properties of our entity
List<PropertyInfo> ourEntityProperties = new List<PropertyInfo>();
foreach (var property in entity.GetType().GetProperties())
{
    // check whether it only belongs to the child
    if (property.DeclaringType.Equals(entity.GetType())) 
    {
        ourEntityProperties.Add(property);
    }       
}

然后只需要一个循环就可以过滤掉所有必要的属性。

在"linqish“一词中,您可以将其写成:

代码语言:javascript
复制
List<PropertyInfo> ourEntityProperties = entity.GetType().GetProperties().Where(x=>x.DeclaringType.Equals(entity.GetType())).ToList();

但看上去很可怕;)

票数 1
EN

Stack Overflow用户

发布于 2017-03-01 15:00:40

PropertyInfo“知道”你以前要它的类型。例如:

代码语言:javascript
复制
using System;
using System.Reflection;

class Base
{
    public int Foo { get; set; }
}

class Child : Base
{    
}

class Test
{
    static void Main()
    {
        var baseProp = typeof(Base).GetProperty("Foo");
        var childProp = typeof(Child).GetProperty("Foo");
        Console.WriteLine(baseProp.Equals(childProp));
        Console.WriteLine(baseProp.ReflectedType);
        Console.WriteLine(childProp.ReflectedType);
    }
}

产出如下:

代码语言:javascript
复制
False
Base
Child

幸运的是,您可以更简单地做到这一点--如果您只想知道在TEntity中声明了哪些属性,只需使用:

代码语言:javascript
复制
var props = typeof(entity.GetType()).GetProperties(BindingFlags.Instance | 
                                                   BindingFlags.Public | 
                                                   BindingFlags.DeclaredOnly);

如果您也想要静态属性,请进行调整。重点是BindingFlags.DeclaredOnly

票数 3
EN

Stack Overflow用户

发布于 2017-03-01 15:16:23

PropertyInfo包含许多属性,其中一些属性的值对于它们所引用的对象类型是唯一的,因此我相信您可能只想检查属性的name属性:

代码语言:javascript
复制
//properties whose names are unique to our Entity
var propertiesToPopulate = ourEntityProperties
    .Where(oep => !entityBaseProperties.Any(ebp => ebp.Name == oep.Name)).ToList();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42534855

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档