.NET中的PHP变量变量 [英] PHP variable variables in .NET

查看:67
本文介绍了.NET中的PHP变量变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.NET是否原生支持类似于PHP的变量变量?

Does .NET natively support anything similar to PHP's variable variables?

如果没有,如何最容易实现 1 ?

If not, how1 could such a feature be most easily implemented?

1 如果您认为变量总是坏的,请随时陈述您的情况,但主要问题是:如何如何实现?

1 If you think variable variables are always bad, feel free to state your case but the main question is: how can they be implemented?

推荐答案

此功能深深地嵌入了 动态语言 . C#的起源是一种静态的,面向对象的语言,直到C#3.0为止,这意味着以任何适当的方式完成您想要的东西都是没有运气的.但是,C#4.0/.NET 4.0引入了 关键字,它可以像在PHP中那样动态键入变量.不幸的是,尽管这是C#成为静态/动态混合语言的飞跃,但它却缺少了几乎所有动态语言都具有的至关重要的eval函数.借助传闻中的C#5.0/.NET 5.0的编译器即服务功能,可以有效地引入此功能(尽管内部行为会有所不同).在此之前,没有一种像样的解决方案,可以使用Dictionary存储变量名.

This is a feature deeply embedded in dynamic languages. C# has its roots as a static, object-oriented language, and up to C# 3.0 this means no luck accomplishing what you want in any proper way. However, C# 4.0/.NET 4.0 introduces the dynamic keyword, which allows variables to be dynamically typed, as in PHP. Unfortunately, although this is a leap forward in the path of C# becoming a static/dynamic hybrid language, it is missing the crucial eval function that almost every dynamic language has. With the rumoured compiler-as-a-service feature of C# 5.0/.NET 5.0, this will effectively be introduced (though the internal behaviour would not be the same). Until then, there's no decent solution short of the hack of using a Dictionary to store variable names.

这篇关于.NET中的PHP变量变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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