如何为现有类创建动态属性。 (system.dynamics新手) [英] How to create dynamic properties for existing class. (New to system.dynamics)

查看:57
本文介绍了如何为现有类创建动态属性。 (system.dynamics新手)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

System.Dynamic命名空间新手。



我试图创建一个类,在实例化时,通过传递给其构造函数的string []生成自己的属性。我对此时分配值不感兴趣,也不生成方法/事件。只是试图了解如何为我的'MyDynamicModel'类创建属性...



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...

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





后来我想在对象的属性上设置/获取值。 ..





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

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



...


...

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



...



最后我计划在我的ASP.NET MVC视图中使用该模型。



我不是在为我的整个网络应用寻找通用模型,但这个模型用于应用程序范围的设置/选项。



感谢您的任何指示!



我尝试过:



我花了几个小时研究DynamicObject Class,尝试从CodeProject中获取样本,解决方案还不明显。


...

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.

推荐答案

你是看错了方向。到目前为止您所描述的所有内容都缩减为使用类 System.Dynamic.ExpandoObject 。考虑一下,例如:



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:

using System.Dynamic;

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



这是大致相当于使用松散型打字范例或动态+鸭子打字规则,就像在JavaScript中一样。



我对在纯.NET系统中实际使用这些场景持怀疑态度。我可以理解使用与某些动态语言的兼容性有效参与某些解决方案的优点,但对于纯.NET而言,它只是缺少严格的输入,没有别的。通过使用关联集合(例如set,dictionary),可以更容易地实现在运行时添加类似属性的元素(在编译期间未定义)的效果。



对于一些更高级的想法,请查看此CodeProject文章:如何为现有类创建动态属性。 (system.dynamics新增) [ ^ ]。



-SA


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


这篇关于如何为现有类创建动态属性。 (system.dynamics新手)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆