我正在打电话:
var properties = person.GetType().GetProperties(BindingFlags.DeclaredOnly |
BindingFlags.Public |
BindingFlags.Instance);
这会返回我想要的内容,除了它之外,它还返回一个我不想要名为Updated的属性,但我可以很容易地忽略它。它还会返回我不想包含的CarReference
和Car
。如何排除这些字段?目前,我有一个排除属性的列表,如果名称与其中一个匹配,我只是跳过它,但我希望它更通用,而不是硬编码"CarReference"
和"Car"
例如
答案 0 :(得分:0)
我不知道这是否是你想要的,但这里有一个可以帮助你的片段。自动生成的Entity Framework“Entity”类的某些属性有标准:
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())
{
// ..
}
答案 1 :(得分:0)
使用&#34;命名空间&#34; PropertyType中列出的属性,外部实体将被以下语句跳过:if(type.Namespace!=&#34; System&#34;)
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
}
}