A dictionary where value is an anonymous type in C#

You can’t declare such a dictionary type directly (there are kludges but these are for entertainment and novelty purposes only), but if your data is coming from an IEnumerable or IQueryable source, you can get one using the LINQ ToDictionary() operator and projecting out the required key and (anonymously typed) value from the sequence elements: … Read more

Anonymous Type vs Dynamic Type

You seem to be mixing three completely different, orthogonal things: static vs. dynamic typing manifest vs. implicit typing named vs. anonymous types Those three aspects are completely independent, they have nothing whatsoever to do with each other. Static vs. dynamic typing refers to when the type checking takes place: dynamic typing takes place at runtime, … Read more

How do you unit test ASP.NET Core MVC Controllers that return anonymous objects?

Original idea from this answer with a more generic approach. Using a custom DynamicObject as a wrapper for inspecting the value via reflection there was no need to add the InternalsVisibleTo public class DynamicObjectResultValue : DynamicObject, IEquatable<DynamicObjectResultValue> { private readonly object value; public DynamicObjectResultValue(object value) { this.value = value; } #region Operators public static bool … Read more

Creating an anonymous type dynamically? [duplicate]

Only ExpandoObject can have dynamic properties. Edit: Here is an example of Expand Object usage (from its MSDN description): dynamic sampleObject = new ExpandoObject(); sampleObject.TestProperty = “Dynamic Property”; // Setting dynamic property. Console.WriteLine(sampleObject.TestProperty ); Console.WriteLine(sampleObject.TestProperty .GetType()); // This code example produces the following output: // Dynamic Property // System.String dynamic test = new ExpandoObject(); ((IDictionary<string, … Read more

How to dynamic new Anonymous Class?

Anonymous types are just regular types that are implicitly declared. They have little to do with dynamic. Now, if you were to use an ExpandoObject and reference it through a dynamic variable, you could add or remove fields on the fly. edit Sure you can: just cast it to IDictionary<string, object>. Then you can use … Read more

C# ‘dynamic’ cannot access properties from anonymous types declared in another assembly

I believe the problem is that the anonymous type is generated as internal, so the binder doesn’t really “know” about it as such. Try using ExpandoObject instead: public static dynamic GetValues() { dynamic expando = new ExpandoObject(); expando.Name = “Michael”; expando.Age = 20; return expando; } I know that’s somewhat ugly, but it’s the best … Read more

Accessing constructor of an anonymous class

From the Java Language Specification, section 15.9.5.1: An anonymous class cannot have an explicitly declared constructor. Sorry 🙁 EDIT: As an alternative, you can create some final local variables, and/or include an instance initializer in the anonymous class. For example: public class Test { public static void main(String[] args) throws Exception { final int fakeConstructorArg … Read more