Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
New to System.Dynamic namespace.

I am attempting to create a class that, when instantiated, generates its own properties via string[] passed to its constructor. I'm not interested in assigning values at this point, nor generating methods/events. Only trying to understand how to create properties for my 'MyDynamicModel' class...

C#
public class MyDynamicModel /* inherit : DynamicObject ? */
{
	MyDynamicModel( string[] properties )
	{
		// magic with ExpandoObject? 
	}
}


Later I want to set/get values on the object's properties...

C#
MyDynamicModel myModel = new MyDynamicModel( new string[] {"Prop0","Prop1","Prop2"} );

...
C#
myModel.Prop0 = "howdy";
myModel.Prop1 = false;
myModel.Prop2 = -0;

...

In the end I plan to use the model in my ASP.NET MVC View.

I'm not looking for a universal model for my entire web app, but this model to be used for application wide settings/options.

Thanks for any direction!

What I have tried:

I've spent hours researching DynamicObject Class, trying samples from CodeProject and the solution isn't yet obvious.
Posted
Updated 10-Apr-16 9:57am
Comments
Sergey Alexandrovich Kryukov 10-Apr-16 15:28pm    
May I ask you: what do you hope to use if for, in terms of ultimate goals?
—SA
Mort Sjostrom 10-Apr-16 15:47pm    
I plan to use it for a small set of MVC5 Views which represent basic settings / options for the web application on which I am developing. Settings that indicate how and what content is rendered, enable/disable various features, how various calculations should compute, default order of grids, more, etc.

These "settings" are stored in one table and depending on the web app feature, various records are pulled from that table and mapped to a model which is bound to a view.

There are so many groups of settings coming from the same source, that I thought I should create one "settings" model wherein the properties would be dynamic, and would not have to create fixed models/dto's/maps for each group of settings. hope that makes sense and open to ideas.
Sergey Alexandrovich Kryukov 10-Apr-16 16:25pm    
I have a strong impression that such features are only used when they are non-dynamic. The dynamic could be used when a user, not you, introduce some new properties. Is it a part of the plan?
Anyway, I expressed my skepticism but also pointed out some basic ideas in Solution 1.
—SA

1 solution

You are looking in wrong direction. All you described so far is reduced to the use of the class System.Dynamic.ExpandoObject. Consider this, for example:

C#
using System.Dynamic;

//...
dynamic myObject = new ExpandoObject();
myObject.SomeProperty = "some string";
myObject.SomeOtherProperty = -13;
myObject.SomeProperty = 13.14; // change of type

This is roughly equivalent to using loose-type typing paradigm or dynamic + duck typing discipline, as in JavaScript.

I'm quite skeptical about practical use of such scenarios in pure-.NET systems. I could understand the merit of using compatibility with some dynamic languages effectively participating in some solution, but for pure .NET it's just the loss of strict typing, nothing else. The effect of adding property-like elements during runtime (not defined during compile time) could be easy achieved in more controllable with with the use of associative collections (such as set, dictionary).

For some more advanced ideas, look at this CodeProject article: How to create dynamic properties for existing class. (New to system.dynamics)[^].

—SA
 
Share this answer
 
v3
Comments
Mort Sjostrom 10-Apr-16 16:47pm    
Thanks Sergey and yours is most helpful insight into a namespace that I've only begun to learn. I'm considering your idea in Solution 1.

One challenge is that over time new properties are introduced (and removed) on my inherited project and I'm very open to other ideas.

I have considered a dictionary which introduces obscure requirements with regard to model binding and a need to specify explicit .Key & Value parameter s. With that, and apart from any one discipline, I've been exploring duck-type dynamics or possibly writing a binder. For now I remain curious about dynamics.
Sergey Alexandrovich Kryukov 10-Apr-16 20:34pm    
You are very welcome.
Good luck, call again.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900