如何使用nameof来获取C#属性中的类中的属性的完全限定名称? [英] How to use nameof to get the fully qualified name of a property in a class in C# Attributes?

查看:436
本文介绍了如何使用nameof来获取C#属性中的类中的属性的完全限定名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在ASP.Net MVC项目中使用Foolproof库,在某些情况下,我需要使用属性检查模型成员类中的属性.

I am using Foolproof library in ASP.Net MVC project and in some cases I need to check a property within a member class of my model using attribues .

例如,我有一个用户类,其属性类型为Address(地址),我需要检查Address(地址)中的City(城市).

For example I have a user class which has a property of type Address and I need to check for the City in the Address.

在我的示例中,属性需要以点符号表示属性名称,您可以说"Address.City".

The attributes need to have the name of the property in a dot notation for my example you could say "Address.City".

如果以后我需要在(地址或城市)上更改名称,当然这会遇到重构问题

Of course this suffers from refactoring issues if I need to change either names later on (Address or City)

我需要为此使用nameof,当然,如果我这样使用它,则当然要使用它:

I need to use nameof for that purpose and of course if I use it like this :

nameof(Address.City) 

它将仅生成城市.

我需要nameof,因为它会产生属性中允许的常量值.

I need nameof because it produces constant values that are allowed in attributes.

我发现参考文献表明这是可能的,但不是. https://msdn.microsoft.com/en-us/library/dn986596. aspx 在备注部分中说:

I found that the reference tells it is possible but not how. https://msdn.microsoft.com/en-us/library/dn986596.aspx in remarks section it says:

如果需要获取全限定名称,则可以将typeof表达式与nameof一起使用.

If you need to get the fully-qualified name, you can use the typeof expression along with nameof.

但是我找不到任何地方可以告诉我该怎么做.

but I couldn't find any place to tell how to do this.

有人可以帮忙吗? 预先感谢您的时间和精力.

Can anyone help, please? Thanks in advance for your time and effort.

更新: 2019年10月

当我再次查阅文档时,他们删除了上面的声明,并替换为.

As I looked up the documentation again they removed the above statement and replaced it with.

如前面的示例所示,在类型和名称空间的情况下,产生的名称通常不是完全限定的.

As the preceding example shows, in the case of a type and a namespace, the produced name is usually not fully qualified.

推荐答案

经过一番挖掘,我发现在开发此功能时已经讨论了此问题. https://roslyn.codeplex.com/discussions/552376 特别是在这里 https://roslyn.codeplex.com/discussions/552377 MgSam的评论

After a bit of digging I found that this issue has been discussed already upon developing this feature in here https://roslyn.codeplex.com/discussions/552376 and specially in here https://roslyn.codeplex.com/discussions/552377 for the comment by MgSam

如所建议的,我可以想象发生这样的事情来得到一个完全限定的名称:BindToFullyQualifiedName(nameof(Microsoft)+." + nameof(Microsoft.Data)+." + nameof(Microsoft.Data.实体)+." + nameof(Microsoft.Data.Entities.EntityObject));

As it is proposed, I can imagine something like this happening to get a fully qualified name: BindToFullyQualifiedName(nameof(Microsoft) + "." + nameof(Microsoft.Data) + "." + nameof(Microsoft.Data.Entities) + "." + nameof(Microsoft.Data.Entities.EntityObject));

答案是

我认为typeof(EntityObject).FullName在您的计算机中已足够 情况.

I think typeof(EntityObject).FullName is fully sufficient in your case.

在结束讨论时,没有进一步评论的另一方式.

Which concluded the discussion with no further comments on another way to do this.

不幸的是,这意味着无法使用用户nameof来直接获取完全限定名称以在Attributes中使用.

Unfortunately this means there is no way to user nameof and get the fully qualified name directly for usage in Attributes.

可能到此为止,我想微软应该更改其文档以使其更加清晰和准确.

Probably this is the end of it and I suppose Microsoft should change their documentation to make it more clear and precise.

这篇关于如何使用nameof来获取C#属性中的类中的属性的完全限定名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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