I am generating a OkObjectResult that has anonymous properties:
When i receive data i can see the properties in the debugger / quickwatch:
I am unable to get the property names out of my anonymous object.
Is there a "simple" way?
The solution i found was to search for my desired properties via reflection. Is this the way to to it?
Edit: Even whey using dynamic i am not able to get the properties.
Instead of var use dynamic type for okResult. Thereafter, you can access your properties like: okResult.data and so on...
Update: As Daisy pointed out below, you will need InternalsVisibleTo because the generated anonymous type is internal, and the dynamic binder checks that.
Related
I have a dynamic type in C# (Content, a type that inherits DynamicObject). It wraps an inner JSON object containing the data. In TryGetMember I return the requested properties from this inner JSON object. This works just fine in case of simple types (e.g. an int or string property), because .Net converts those values correctly.
But I want to use properties with more complex types:
dynamic content = Content.Load(id);
IEnumerable<Book> bookList = content.Books;
The problem is that in TryGetMember of my class I have no way to know the type that I should convert to (in this case the IEnumerable Book), because binder.ReturnType is always Object. According to this article, this is the normal behavior:
Determining the expected type of a DynamicObject member access
But I find this very hard to believe: how is it possible that the API does not let me know the target type? This way I will have to force developers to use the method syntax to specify the type explicitely:
IEnumerable<Books> bookList = content.Books<IEnumerable<Book>>();
...which is ugly and weird.
You could store Type data alongside the JSON serialized data, but that seems like a rather inefficient method of accomplishing what you're trying to do. If your content isn't truely dynamic (e.g., the content changes, but the basic schema of the object is the same), you could just have precompiled classes for each JSON object type, and serialize the JSON into that class once you receive the data. This way, all of the type data would already be recognized by the compiler at runtime.
It turns out that this is not possible indeed. I ended up creating an extension method (defined in two forms: for dynamic collections and JArrays). That way I can at least get a collection of my Content class and all of the following solutions work:
One line, but a bit long
var members = ((IEnumerable<dynamic>)group.Members).ToContentEnumerable();
Separate lines
IEnumerable<dynamic> members = adminGroup.Members;
foreach (dynamic member in members.ToContentEnumerable())
{
//...
}
Using the method call syntax of the extension method (a bit unusual).
var members = ContentExtensions.ToContentEnumerable(group.Members);
So, I have my own mapper (NOT AutoMapper) which maps models to each other. You give the model you'd like to map to, and with the Map method you push an object in. Beside of this I wrote the Extend method which functions as a override for the Map method to, for example, add properties which are not available in the object being mapped.
Problem:
The problem hereby is that my public Mapper<T> Extend(Func<T, T> func) method doesn't like the different types.
Possible solutions:
There are 2 solutions I'm thinking of:
Ignore the error and map the value within my Extend method. Which isn't possible as far as I know due to the expression being executed immediately.
Create a LINQ method which maps the value for me. Eg; q => q.Ownership = obj.Ownerships.First().Map().
Question:
How can I resolve this error and achieve what I want?
I have a RESTful service which returns JSON that I am deserialising into classes in c#.
I need to map some of the properties from the deserialised object model into properties in a different class.
However, I would like to do this through an (xml?) config file which can specify the from/to property names, so that mappings can be changed without recompiling code.
For example:
objectA.Name.FirstName = objectB.FirstName
objectA.Name.LastName = objectB.LastName
What is the best way to do this?
You could let something like AutoMapper do the mapping for you.
There are samples in the source code and configuration options in the wiki.
If you want it to to be based on late binding you can use reflection to dynamically execute the property assignments based on xml definitions.
You can see some examples in this asnwer: Set object property using reflection
Lately I have been using reflection to work in my project, and I have the current question.
While in Type.GetProperties(Flags), we can filter the properties we get using 'Flags'; in TypeDescriptor.GetProperties(), we don't.
In type.GetProperties I can filter to get only properties not inherited.
Is it possible to do the same with TypeDescriptor.GetProperties() (only properties not inherited)?
Thank you
No, you can't.
The TypeDescriptor.GetProperties() is used to get PropertyDescriptor instances with possibility to filter using specific Attributes.
The Type.GetProperties() is used to get PropertyInfo instances with possibility to filter using specific BindingFlags.
Hey guys. I have the following situation.
I want to use a TypeDescriptor to get the properties of a certain type. The type's depth in the inheritance hierarchy may vary. I only want to get the properties declared in the type itself and not in its parents (base). The problem is that when I call TypeDescriptor.GetProperties() it would return everything declared up the inheritance hierarchy up to Object.
I only saw that I can filter the output by Attributes, but I don't want to add another attribute to the properties in my types just for this. Getting them through reflection and not using TypeDescriptor would do what I want, but is not an option for me, because some of the properties will be added dynamically to the type at some point.
Any ideas? If the question is not clear I could provide an example.
You can filter the properties using the ComponentType property :
var properties = from p in TypeDescriptor.GetProperties(x).Cast<PropertyDescriptor>()
where p.ComponentType == x.GetType()
select p;
Can't you just modify the implementation of ICustomTypeDescriptor to reflect your desired behavior?