![](/img/trans.png)
[英]How can I exclude Foreign Entities when calling GetProperties on an Entity?
[英]How to avoid returning certain properties and Foreign Entities when calling GetProperties on an Entity?
我正在打电话:
var properties = person.GetType().GetProperties(BindingFlags.DeclaredOnly |
BindingFlags.Public |
BindingFlags.Instance);
这返回了我想要的东西,它还返回了一个我不想要的属性,称为更新,但是我可以轻松地忽略它。 它还返回我不想包括的CarReference
和Car
。 如何排除这些字段? 当前,我有一个排除属性的列表,如果名称与其中之一匹配,我将跳过它,但是我希望它更通用,而不是硬编码"CarReference"
和"Car"
我不知道这是否是您要寻找的内容,但这是一个可以帮助您的代码段。 对于自动生成的实体框架“实体”类的某些属性,有一些标准:
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())
{
// ..
}
使用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.