简体   繁体   English

如何获取实体中所有属性的名称?

[英]How to get all names of properties in an Entity?


What I'm trying to do is to pass an entity object to method and return all the names of the properties in it.我要做的是将实体 object 传递给方法并返回其中属性的所有名称。
I'm using this code to get all the props names:我正在使用此代码来获取所有道具名称:

return classObject.GetType().GetProperties();

The problem is that this code return "EntityKey" and "EntityState" as properties whe I use it with Entity Object.问题是当我将它与实体 Object 一起使用时,此代码返回“EntityKey”和“EntityState”作为属性。
Is there any way to do it?有什么办法吗?

Thanx in advance提前感谢

You want all direct properties, but not the properties of the base type, which in your case is EntityObject :您想要所有直接属性,但不想要基本类型的属性,在您的情况下是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));

This sample assumes there is a base type (which is the case for DB first), refactor when that is not guaranteed.此示例假定存在基本类型(首先是 DB 的情况),如果不能保证则进行重构。

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:从@Matt 的评论中编辑:以上所有内容都是不必要的,可能会因为没有想到这一点而打我的头 - 只需使用正确的绑定标志:

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

It is also possible without reflection:也可以不用反射:

using (var context = new ModelContainer())
{
    // Access CSDL
    var container = context.MetadataWorkspace
                           .GetEntityContainer(context.DefaultContainerName, DataSpace.CSpace);
    // Access name of related set exposed on your context
    var set = container.BaseEntitySets[context.YourEntitySet.EntitySet.Name];
    // Access all properties
    var properties = set.ElementType.Members.Select(m => m.Name).ToList();
    // Access only keys
    var keys = set.ElementType.KeyMembers.Select(m => m.Name).ToList();
}

As you can see you have access to much more then names.如您所见,您可以访问更多的名称。 The example shows that you can now which property is part of key.该示例显示您现在可以知道哪个属性是键的一部分。 If you access Members directly you can know which property is scalar, complex type or navigation property.如果您直接访问Members ,您可以知道哪个属性是标量、复杂类型或导航属性。

All information are already loaded so there is no need for reflection.所有信息都已加载,因此无需反思。 If you want to use reflection don't forget to use it only once (first time you need it) and then store and reuse received property names.如果您想使用反射,请不要忘记只使用一次(第一次需要它),然后存储并重用收到的属性名称。 Reflection is slow so using it each time you need names is a bad practice.反射很慢,因此每次需要名称时都使用它是一种不好的做法。

I had the same problem.我有同样的问题。 The solution I found was to create an array with the name of the properties to return (I olnly need a few).我找到的解决方案是创建一个带有要返回的属性名称的数组(我只需要一些)。 In your case, since it can be laborious to keep track of all properties, I would filter the properties EntityKey and EntityState and return all the others.在您的情况下,由于跟踪所有属性可能很费力,因此我将过滤属性EntityKeyEntityState并返回所有其他属性。 The code would be something like this:代码将是这样的:

public IEnumerable<PropertyInfo> GetProperties()
{
    Type t = this.GetType();

    return t.GetProperties()
        .Where(p => (p.Name != "EntityKey" && p.Name != "EntityState"))
        .Select(p => p).ToList();
}

Don't know if there is a better solution, but it would be nice;) Hope it helps!不知道是否有更好的解决方案,但它会很好;)希望它有帮助!

As stated by BrokenGlass but beware if you need performance and you want to do this in loops.正如 BrokenGlass 所述,但如果您需要性能并且想要在循环中执行此操作,请注意。 Reflection is not a fast thing.反思不是一件快速的事情。

If you need performance you may wish to put a virtual method in your base class to retrieve the properties as an array of strings or whatever, and override that in all derived classes.如果您需要性能,您可能希望在基础 class 中放置一个虚拟方法,以将属性作为字符串数组或其他内容检索,并在所有派生类中覆盖该属性。 This would be the fastes approach but with more coding.这将是最快的方法,但需要更多的编码。

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

相关问题 如何在 json object 中获取所有属性的名称(没有它们的值)? - How to get the names of all properties (without their values) in a json object? 如何获取Linq to Entity Datamodel(带有一个表)中所有列的名称? - How to get the Names of all Columns in a Linq to Entity Datamodel (with one table)? 如何从RavenDB数据库中获取所有实体名称(集合名称)? - How can I get all entity names (collection names) from a RavenDB database? 从 class 获取所有数据表属性的所有列名 - Get all column names of all datatable properties from a class 如何获取所选属性的名称? - How to Get the Names of Only the Selected Properties? 如何从对象参数获取属性名称? - How to get properties Names from object parameter? 如何获取数据库表实体框架6的所有属性和属性显示名称? - how can i get all properties and attribute display name of database table entity framework 6? 遍历实体对象的所有属性并获取相应的值? - Loop through all the properties of an entity object and get the corresponding value? 如何在运行时在Entity Framework Code First中获取实体类的属性 - How to get the properties of entity class in Entity Framework Code First at runtime 获取实体的属性列表 - Get list of properties of an entity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM