[英]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. 它还返回我不想包括的CarReference
和Car
。 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.