Reputation: 19821
I probably want too much, but my scenario is
public dynamic CreateConfigObject(JobConfigurationModel config) {
dynamic configObject = new { };
configObject.Git = new GitCheckout {
Repository = config.Github.Url
};
return configObject;
}
Of course, it fails on configObject.Git
since this property does not exist. I want to be able to add any number of properties at run time, with out any beforehand knowledge of number and names of properties;
Is such case possible in C# at all, or my ill JavaScript imagination starts to hurt me? :)
Upvotes: 16
Views: 10042
Reputation: 887195
dynamic
allows loosely-typed access to strongly-typed objects.
You should use the ExpandoObject
class, which allows loosely-typed access to an internal dictionary:
dynamic configObject = new ExpandoObject();
configObject.Git = new GitCheckout {
Repository = config.Github.Url
};
Upvotes: 35