How to use nameof to get the fully qualified name of a property in a class in C# Attributes?

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 .

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.

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)

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

nameof(Address.City)

it will produce City Only.

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

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:

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.

Update : October-2019

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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

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));

The answer was

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.

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.

Method 2

If you don’t want to specify a “reference” type (not ‘reference type’ in the C# sense) to get your target namespace (why should you?), and to save putting a string of nameof operators separated by ., then you can include the “base” namespace by getting the assembly name if it follows the default convention and is named the same as your default namespace, as thus:

$"{Assembly.GetExecutingAssembly().GetName().Name}.{nameof(Level1Namespace)}.{nameof(Level2Namespace)}"

This will at least save you some code, and protect you against refactors where you change the assembly name or even move an entire hierarchy of classes to another assembly, but it of course won’t protect against changes within the namespace hierarchy itself. To protect against that, then I think you will indeed need a “reference” type such as perhaps an abstract class or interface that all other classes in the desired namespace inherit, and then apply the accepted answer solution to it.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x