문제

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);
도움이 되었습니까?

해결책

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);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top