简体   繁体   English

在实体上调用GetProperties时如何避免返回某些属性和外来实体?

[英]How to avoid returning certain properties and Foreign Entities when calling GetProperties on an Entity?

I am making the call: 我正在打电话:

var properties = person.GetType().GetProperties(BindingFlags.DeclaredOnly |
                                                        BindingFlags.Public |
                                                        BindingFlags.Instance);

This returns what I want except for, it also returns a property I don't want called Updated, but I can easily just ignore this. 这返回了我想要的东西,它还返回了一个我不想要的属性,称为更新,但是我可以轻松地忽略它。 It also returns CarReference and Car which I don't want to include. 它还返回我不想包括的CarReferenceCar How can I exclude these fields? 如何排除这些字段? Currently, I have a list of excluded properties and if the name matches one of those, I just skip over it, but I want it to be more generic instead of hard-coding "CarReference" and "Car" for example 当前,我有一个排除属性的列表,如果名称与其中之一匹配,我将跳过它,但是我希望它更通用,而不是硬编码"CarReference""Car"

I don't know if this is what you are looking for, but here is a snippet which could help you. 我不知道这是否是您要寻找的内容,但这是一个可以帮助您的代码段。 There are criterions for some properties of automatically generated Entity Framework "Entity" classes: 对于自动生成的实体框架“实体”类的某些属性,有一些标准:

var flags = BindingFlags.Public | BindingFlags.Instance;
var destinationProperties = typeof(TDestination).GetProperties(flags);

foreach (var property in destinationProperties)
{
    var type = property.PropertyType;

    // Ignore reference property (e.g. TripReference)
    if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(EntityReference<>))
    {
        // ...
    }

    // Ignore navigation property (e.g. Trips)
    if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(EntityCollection<>))
    {
        // ...
    }

    // Ignore ID (edmscalar) property (e.g. TripID)
    if (property.GetCustomAttributes(typeof(EdmScalarPropertyAttribute), false).Any())
    {
        // ..
    }

Use the "Namespace" property listed in the PropertyType, Foreign Entities would be skipped by the following statement: if (type.Namespace != "System") 使用PropertyType中列出的“名称空间”属性,将通过以下语句跳过外部实体:if(type.Namespace!=“ System”)

        using (var db = new Entities.YourEntities())
        {
            var originalObj = db.MyTable.FirstOrDefault();

            foreach (var prop in originalObj.GetType().GetProperties())
            {
                var type = prop.PropertyType;

                if (type.Namespace != "System")
                    continue;

                var name = prop.Name;
                var value = prop.GetValue(originalObj, null);

                //your code here
            }
        }

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

相关问题 在实体上调用GetProperties时如何排除外来实体? - How can I exclude Foreign Entities when calling GetProperties on an Entity? 使用GetProperties时如何区分导航属性和常规属性? - How to distinguish Navigation Properties from regular ones when using GetProperties? Type.GetProperties不返回任何属性 - Type.GetProperties not returning any properties 实体框架:添加到新实体时,如何使用来自实体B的自动生成ID作为实体A中的外键? - Entity framework: When adding to new entities, how to use autogen ID from entity B as foreign key in entity A? 反射GetProperty应该排除某些数据类型上不需要的子级属性 - Reflection GetProperties should exclude unwanted children properties on certain datatype 域实体中的外键属性 - Foreign key properties in domain entities 如何使用外部实体链实现NHibernate批量插入实体 - How to implement NHibernate batch insert entity with chain of foreign entities 实体框架在未访问导航属性时延迟加载相关实体 - Entity Framework is lazy loading related entities when nav properties are not accessed 实体映射:将两个属性引用到外键时出现问题 - Entity mapping: Problems when referencing two properties to a foreign key 将实体属性映射到其他实体 - Mapping entity properties to other entities
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM