如何获得在实体属性的所有名称? [英] How to get all names of properties in an Entity?

查看:103
本文介绍了如何获得在实体属性的所有名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

结果
我想要做的是一个实体对象传递给方法,并在它返回的所有属性的名称。结果
我使用这个code把所有的道具名称:


What I'm trying to do is to pass an entity object to method and return all the names of the properties in it.
I'm using this code to get all the props names :

return classObject.GetType().GetProperties();

问题是,这个code回报的EntityKey和EntityState为属性磨片我和实体对象使用它。结果
有没有办法做到这一点?

The problem is that this code return "EntityKey" and "EntityState" as properties whe I use it with Entity Object.
Is there any way to do it ?

感谢名单提前

推荐答案

你希望所有的直接属性,但不是基本类型,它在你的情况的属性 EntityObject

var type = classObject.GetType();
//alternatively call out directly: typeof(EntityObject).GetProperties()...
var basePropertyNames = type.BaseType.GetProperties().Select(x => x.Name);
var props = type.GetProperties().Where(p => !basePropertyNames.Contains(p.Name));

本示例假定有一个基本类型(这是DB的情况下第一),重构时无法得到保证。

This sample assumes there is a base type (which is the case for DB first), refactor when that is not guaranteed.

修改从@马特的评论:以上全部是不必要的,可以拍我的头不这种思维 - 只要使用正确的绑定标志:

Edit from @Matt's comment: All of the above is unnecessary, could slap my head for not thinking of this - just use the right binding flags:

return classObject.GetType().GetProperties(BindingFlags.DeclaredOnly | 
                                           BindingFlags.Public | 
                                           BindingFlags.Instance);

这篇关于如何获得在实体属性的所有名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆