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# object initialization of read only collection properties

This: MyClass c = new MyClass { StringCollection = { “test2”, “test3” } }; is translated into this: MyClass tmp = new MyClass(); tmp.StringCollection.Add(“test2”); tmp.StringCollection.Add(“test3”); MyClass c = tmp; It’s never trying to call a setter – it’s just calling Add on the results of calling the getter. Note that it’s also not clearing the … Read more

Metadata file ‘.dll’ could not be found

I just had the same problem. Visual Studio isn’t building the project that’s being referenced. Written Instructions: Right click on the solution and click Properties. Click Configuration on the left. Make sure the check box under “Build” for the project it can’t find is checked. If it is already checked, uncheck, hit apply and check … Read more

Recursive control search with LINQ

Take the type/ID checking out of the recursion, so just have a “give me all the controls, recursively” method, e.g. public static IEnumerable<Control> GetAllControls(this Control parent) { foreach (Control control in parent.Controls) { yield return control; foreach(Control descendant in control.GetAllControls()) { yield return descendant; } } } That’s somewhat inefficient (in terms of creating lots … Read more

Why must a lambda expression be cast when supplied as a plain Delegate parameter

A lambda expression can either be converted to a delegate type or an expression tree – but it has to know which delegate type. Just knowing the signature isn’t enough. For instance, suppose I have: public delegate void Action1(); public delegate void Action2(); … Delegate x = () => Console.WriteLine(“hi”); What would you expect the … Read more