建议:nameof(xyz)运算符 [英] Suggestion: nameof(xyz) operator

查看:48
本文介绍了建议:nameof(xyz)运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近(重新)在Windows窗体中发现了数据绑定,这要归功于它在Visual Studio 2005中的
进展。然而,当我看起来更深一点时,我实现了b $ b它仍然有一种令人厌烦的倾向,即在我的代码中粘贴一整套文字字符串。坦率地说,我认为它们是由Visual

Studio附带的RAD设计师对开发人员施加的b
瘟疫。使用文字字符串来引用控件,数据,b $ b源,对象等及其属性的问题非常明显:如果其中任何一个

都是手动更改的代码,或者如果程序员无意中从其中一个魔术字符串中删除了一个

字母,那么在运行

应用程序并且这段代码执行最多之前,没有人会注意到通常会带来不愉快的后果(通常是某种类型的丑陋异常)。


如果我们可以使用一些构造,问题可以完全消除

,可以在编译时由编译器验证。在这种情况下,如果发生了错误的
错误,那么该项目就不会构建。不幸的是,就像现在的情况一样,现在似乎无法做到这一点。


正如我想的那样,我想到了更多解决方案

不应该太难提供。所需要的只是一个新的运算符,

编译器可以用非常相同的方式评估

typeof(ClassName)的工作原理。我会称这个运算符为

nameof(ClassName.PropertyName)或者也许是nameof(objectName.PropertyName),

或类似的东西。在我看来,编译器可以轻松访问

这类信息,否则就无法构建我的代码。


如果存在这样的构造,我可以重写这一行:

new System.Windows.Forms.Binding(" EditValue",someObject," LastName",true)


看起来像这样

新的System.Windows.Forms.Binding(nameof(myTextBox.Text),someObject,

nameof(someObject.LastName),true )


并且完全可以编译时验证。


这个概念不仅可以用于数据绑定,还可以用于许多其他的/>
上下文设计师当前坚持文字字符串。它还可以以不同的方式实现
,不仅返回属性的名称或给定类的
方法,而且实际上返回可能的完整描述符

用于进一步反思。


我想在此对此进行一些评论。是否有技术原因

为什么这样的结构无法添加到语言中?


Andrew

I recently (re)discovered data binding in Windows Forms thanks to its
advances in Visual Studio 2005. As I looked a little deeper, however, I
realize that it still suffers from an irksome tendency to stick a whole
bunch of literal strings in my code. Quite frankly, I consider them a
plague imposed on developers by the RAD designers that come with Visual
Studio. The problem with using literal strings to refer to controls, data
sources, objects, etc. and their properties is quite obvious: if any of them
are changed by hand in code, or if the programmer onadvertently removes a
letter from one of such magic strings no one will notice until the
application is run and this very piece of code gets executed most often with
unpleasant consequences (typically some type of an ugly exception).

The problem could be completely eliminated if we could use some construct
that could be verified by the compiler at compile time. In this case if a
typo occurred the project simply wouldn''t build. Unfortunately, as things
stand right now there appears to be no way of doing this.

As I thought about it some more it occurred to me that the solution
shouldn''t be too hard to provide. All that is needed is a new operator that
the compiler could evaluate in very much the same fashion that
typeof(ClassName) works. I would call this operator
nameof(ClassName.PropertyName) or perhaps nameof(objectName.PropertyName),
or something similar. It seems to me that the compiler has easy access to
this type of information, or else it wouldn''t be able to build my code.

If such a construct existed I could rewrite this line:
new System.Windows.Forms.Binding("EditValue", someObject, "LastName", true)

to look like this
new System.Windows.Forms.Binding(nameof(myTextBox.Text ), someObject,
nameof(someObject.LastName), true)

and be entirely compile-time verifiable.

This same concept could be used not only in data binding but in many other
contexts where the designers currently stick literal strings. It could also
be implemented differently to return not just the name of the property or
method of a given class, but actually return the full descriptors that could
be used for further "reflection".

I would like to here some comments about this. Is there a technical reason
why such a construct cannot be added to the language?

Andrew

推荐答案

这听起来是一个很好的主意,虽然你的例子是错误的。我想要
预计


新的System.Windows.Forms.Binding(nameof(myTextBox.Text),someObject,

nameof (someObject.LastName),true);


相当于


新的System.WindowsForms.Binding(" Text",someObject ,LastName,真实);


也许你想要myTextBox的名字,你已经可以这样做了

这个:


新的System.WindowsForms.Binding(myTextBox.Name,someObject," LastName",

true);


但是,对于对象属性名称仍然没有帮助。


我喜欢你的想法,因为我有很多对象到控件的绑定,并且

这将是一个巨大的帮助。


您是否尝试过以下建议:

http://lab.msdn.microsoft.com/productfeedback/


?当您将其发布到反馈网站时,请在此处发布反馈编号

,以便我们可以投票支持。

This sounds like an excellent idea, although your example is wrong. I
would expect

new System.Windows.Forms.Binding(nameof(myTextBox.Text ), someObject,
nameof(someObject.LastName), true);

to be equivalent to

new System.WindowsForms.Binding("Text", someObject, "LastName", true);

Perhaps you wanted the Name of myTextBox, which you can already do like
this:

new System.WindowsForms.Binding(myTextBox.Name, someObject, "LastName",
true);

However, that still doesn''t help with the object property name.

I love your idea because I have lots of object-to-control bindings, and
this would be a huge help.

Have you tried putting in this suggestion at:

http://lab.msdn.microsoft.com/productfeedback/

? When you do post it to the feedback site, post the feedback number
here, so that we can go and vote for it.


Jedrzej Miadowicz写道:
Jedrzej Miadowicz wrote:
我最近(重新)在Windows窗体中发现了数据绑定,这要归功于它在Visual Studio 2005中的进步。然而,我看起来更深一些,但是,<意识到它仍然有一种令人厌烦的倾向,即在我的代码中粘贴一大堆文字字符串。坦率地说,我认为它们是由Visual
Studio附带的RAD设计师强加给开发人员的瘟疫。使用文字字符串来引用控件,数据源,对象等及其属性的问题非常明显:如果其中任何一个
在代码中手动更改,或者程序员无意中删除了来自其中一个魔法字符串的信件,在运行
应用程序之前没有人会注意到这个代码最常被执行并带来令人不快的后果(通常是某种类型的丑陋异常)如果我们可以使用一些可以在编译时由编译器验证的构造,问题可以完全消除。在这种情况下,如果发生错误,项目根本就不会建造。不幸的是,正如现在的情况一样,似乎无法做到这一点。

在我想到这一点时,我想到了解决方案
不应该''太难提供了。所需要的只是一个新的运算符,编译器可以用非常相同的方式评估
typeof(ClassName)的工作方式。我会称这个运算符为/ nameof(ClassName.PropertyName)或者名称为(objectName.PropertyName),
或类似的东西。在我看来,编译器可以轻松访问这类信息,否则它就无法构建我的代码。

如果存在这样的构造,我可以重写这一行:
新的System.Windows.Forms.Binding(" EditValue",someObject," LastName",true)

看起来像这样

新的System.Windows.Forms.Binding(nameof(myTextBox.Text),someObject,
nameof(someObject.LastName),true)

并且完全可以编译时验证。

同样的概念不仅可用于数据绑定,还可用于许多其他设计师目前坚持文字字符串的环境中。它也可以以不同的方式实现,不仅返回属性的名称或给定类的方法,而且实际上返回可以用于进一步反射的完整描述符。 。

我想在此对此进行一些评论。是否有技术原因
为什么这样的结构不能添加到语言中?

Andrew
I recently (re)discovered data binding in Windows Forms thanks to its
advances in Visual Studio 2005. As I looked a little deeper, however, I
realize that it still suffers from an irksome tendency to stick a whole
bunch of literal strings in my code. Quite frankly, I consider them a
plague imposed on developers by the RAD designers that come with Visual
Studio. The problem with using literal strings to refer to controls, data
sources, objects, etc. and their properties is quite obvious: if any of them
are changed by hand in code, or if the programmer onadvertently removes a
letter from one of such magic strings no one will notice until the
application is run and this very piece of code gets executed most often with
unpleasant consequences (typically some type of an ugly exception).

The problem could be completely eliminated if we could use some construct
that could be verified by the compiler at compile time. In this case if a
typo occurred the project simply wouldn''t build. Unfortunately, as things
stand right now there appears to be no way of doing this.

As I thought about it some more it occurred to me that the solution
shouldn''t be too hard to provide. All that is needed is a new operator that
the compiler could evaluate in very much the same fashion that
typeof(ClassName) works. I would call this operator
nameof(ClassName.PropertyName) or perhaps nameof(objectName.PropertyName),
or something similar. It seems to me that the compiler has easy access to
this type of information, or else it wouldn''t be able to build my code.

If such a construct existed I could rewrite this line:
new System.Windows.Forms.Binding("EditValue", someObject, "LastName", true)

to look like this
new System.Windows.Forms.Binding(nameof(myTextBox.Text ), someObject,
nameof(someObject.LastName), true)

and be entirely compile-time verifiable.

This same concept could be used not only in data binding but in many other
contexts where the designers currently stick literal strings. It could also
be implemented differently to return not just the name of the property or
method of a given class, but actually return the full descriptors that could
be used for further "reflection".

I would like to here some comments about this. Is there a technical reason
why such a construct cannot be added to the language?

Andrew



Hm ... saomething告诉我你可以用反射做到这一点......基本上你传递了一个属性并且

你想得到它的名字作为一个字符串...我明天会看看。 ..


Hm... saomething tells me that you can do this using reflection... Basicly you pass a property and
you want to get it''s name as a string... i''ll take a look tomorrow...


MuZZy< tn*@newsgroups.nospam>写道:
MuZZy <tn*@newsgroups.nospam> wrote:
嗯... saomething告诉我你可以用反射做到这一点......
你基本上传递了一个属性,你想把它的名字作为一个
字符串...明天我会看一下......
Hm... saomething tells me that you can do this using reflection...
Basicly you pass a property and you want to get it''s name as a
string... i''ll take a look tomorrow...




不,你不能用反射来做 - 如果你通过了财产为了
的东西,它不会通过PropertyInfo,它会评估该物业

并将*结果*传递给你传递给它的任何东西。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet

如果回复该群组,请不要给我发邮件



No, you couldn''t do that using reflection - if you pass the property to
something, it won''t pass the PropertyInfo, it''ll evaluate the property
and pass the *result* to whatever you''re passing it to.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


这篇关于建议:nameof(xyz)运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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