How can I use FastMember to get the properties of a dynamic object?

I have the following object:

dynamic person = new {Id = 1, Name = "SpiderMan"}; 

I need to be able to iterate over property names, for example. "Id" , "Name" .

I also need to be able to achieve this in the most efficient way, so I decided to use FastMember , however this api does not allow me to iterate through the properties.

Any ideas?

[UPDATE]

Thanks to Marc, I managed to achieve what I wanted to use:

 dynamic person = new { Id = 1, Name = "SpiderMan" }; MemberSet members = TypeAccessor.Create(person.GetType()).GetMembers(); foreach (Member item in members) { // do whatever } 
+5
source share
1 answer

In the script shown, TypeAccessor.Create(obj.GetType()) and GetMember() should work fine, as this type is great for reflection.

In a more general case: a fair question - I honestly cannot remember whether FastMember provides this for true dynamic types, but one important consideration here is that by the very nature of dynamic objects, the set of properties may not even be enumerable - that is, the code may respond to obj.Whatever on the fly, not knowing about Whatever in advance. However, for the object that you actually have, a simple reflection is your best bet. The script you are showing does not require dynamic .

+6
source

All Articles