dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
19.02k stars 4.03k forks source link

pathof operator #14474

Closed adamabmsft closed 7 years ago

adamabmsft commented 8 years ago

The existing nameof operator is great, but it only returns the name of the leaf node of the path provided.

For example:

    nameof(ObservableAccount.Customer.BasicInfo.Name); 
    // evaluates to "Name"

I would like an operator, for instancepathof, that lets me do something like this:

    pathof(ObservableAccount.Customer.BasicInfo.Name); 
    // evaluates to "ObservableAccount.Customer.BasicInfo.Name"

Today I have to write code like this:

    $"{nameof(ObservableAccount.Customer)}.{nameof(ObservableCustomer.BasicInfo)}.{nameof(BasicInfo.Name)}";

or implement a helper that lets me do this:

    PropertyPathHelper.Combine(
        nameof(ObservableAccount.Customer), 
        nameof(ObservableCustomer.BasicInfo), 
        nameof(BasicInfo.Name));
adamabmsft commented 8 years ago

PS - I realize there might be some objection to the choice fullnameof. That's just the first thing I could think of. I'm sure there's a better name.

alrz commented 8 years ago

Since this is only applicable to types I think it's better to just treat typeof(T).FullName as a constant. There is an issue for that but I'm not able to find it at the moment.

yume-chan commented 8 years ago

I only found #3759 and #7973, a comment says there is an issue about FullNameOf on CodePlex but I didn't find it.

10972 also mentioned about "treat typeof(T).FullName as a constant" and "fullnameof()".

jveselka commented 8 years ago

Since this is only applicable to types

Why not members? fullnameof(System.Console.WriteLine)

I wonder however, what do you expect to get from fullnameof(IO.Stream). Should it be "IO.Stream" or "System.IO.Stream"

adamabmsft commented 8 years ago

My suggestion of fullnameof added some confusion here. I don't want an alternate way to find the full name of a type. If you look at my example it's not trying to resolve Namespace.TypeName, but rather it's trying to resolve identifier.PropertyName.SubPropertyName.EvenDeeperPropertyName.

FWIW - My scenario is for deep observability. It's sort of like how in WPF you can data bind a text box to a text value that is nested deep within a root view model. I will end up parsing the property path and setting up listeners down the chain.

I've updated the title and original post to refer to this idea as pathof.

gafter commented 7 years ago

We are now taking language feature discussion in other repositories:

Features that are under active design or development, or which are "championed" by someone on the language design team, have already been moved either as issues or as checked-in design documents. For example, the proposal in this repo "Proposal: Partial interface implementation a.k.a. Traits" (issue 16139 and a few other issues that request the same thing) are now tracked by the language team at issue 52 in https://github.com/dotnet/csharplang/issues, and there is a draft spec at https://github.com/dotnet/csharplang/blob/master/proposals/default-interface-methods.md and further discussion at issue 288 in https://github.com/dotnet/csharplang/issues. Prototyping of the compiler portion of language features is still tracked here; see, for example, https://github.com/dotnet/roslyn/tree/features/DefaultInterfaceImplementation and issue 17952.

In order to facilitate that transition, we have started closing language design discussions from the roslyn repo with a note briefly explaining why. When we are aware of an existing discussion for the feature already in the new repo, we are adding a link to that. But we're not adding new issues to the new repos for existing discussions in this repo that the language design team does not currently envision taking on. Our intent is to eventually close the language design issues in the Roslyn repo and encourage discussion in one of the new repos instead.

Our intent is not to shut down discussion on language design - you can still continue discussion on the closed issues if you want - but rather we would like to encourage people to move discussion to where we are more likely to be paying attention (the new repo), or to abandon discussions that are no longer of interest to you.

If you happen to notice that one of the closed issues has a relevant issue in the new repo, and we have not added a link to the new issue, we would appreciate you providing a link from the old to the new discussion. That way people who are still interested in the discussion can start paying attention to the new issue.

Also, we'd welcome any ideas you might have on how we could better manage the transition. Comments and discussion about closing and/or moving issues should be directed to https://github.com/dotnet/roslyn/issues/18002. Comments and discussion about this issue can take place here or on an issue in the relevant repo.

I am not moving this particular issue because I don't have confidence that the LDM would likely consider doing this.