I would like to write something similar to the following:
// I will pass in a number of "properties" specified as strings that I want modified
string[] properties = new [] { "AllowEdit", "AllowDelete" };
// Casting the component I'm using to a dynamic object of some sort ?
dynamic d = myGridComponent;
// Iterate over the strings and set the properties
foreach(var s in properties)
{
//d.s = true; //
//d[s] = true; // this format would be ideal
}
I was wondering if there was an easy way to do this without using Reflection [.GetProperty(...).GetValue(...,...)] using the new C# 4.0 keyword: dynamic.
It seems that there may be some way, … I’m just not sure of the exact mechanism, and haven’t been able to find the right resource to put all the pieces together.
Thoughts ?
[EDIT]
It looks like there is a package called “Clay” that implements this type of functionality in some way.
Clay on CodePlex
Scott Hanselman on the Subject
It can be done. You just have to override
TryGetIndexonDynamicObject. I needed something similar to invoke static members of a type, but hopefully you will get the idea. Mind you this does not currently work on methods with generic type arguments or methods that are overloaded, limiting its utility: