Domanda

I want to enumerate all object properties using reflection but i want to exclude the properties that reference objects (this should be fast enough because i'm using in a caching solution using Redis/Booksleve).

Currently i have the following but this return all the object properties including instance members:

var propertyNameAndValues = member.GetType().GetProperties().Where(m => m.GetGetMethod() != null).ToDictionary(i => i.Name, i => Encoding.UTF8.GetBytes(i.GetGetMethod().Invoke(member, null).ToString()));
var task = conn.Hashes.Set(db, string.Format("members:{0}", member.id), propertyNameAndValues);
È stato utile?

Soluzione

Use the overload of GetProperties where you can specify a BindingFlags argument and ensure BindingFlags.Static is included but BindingFlags.Instance is excluded.

For example:

var flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static;
var properties = member.GetType().GetProperties(flags);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top