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. 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
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")
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
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.