Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[API Proposal]: Add CallerType attribute to tell the method the current type that is invoking the current member. #102725

Closed
nathan130200 opened this issue May 27, 2024 · 2 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Runtime

Comments

@nathan130200
Copy link

nathan130200 commented May 27, 2024

Background and motivation

As the title says, the introduction of the attribute CallerTypeAttribute which will provide the method with the the type that is invoking the current member, as well as CallerMemberNameName.

I had this problem while writing a micro logger to handle tasks with more logging details. And when I realized that I was either creating a gigantic work-around or creating scoped loggers that store the methodbase instance or method name + type name.

And as .NET does not support expansive macros syntax like C++, it is not possible to simply write a macro that calls all the necessary functions by passing MethodBase.GetCurrentMethod() in each of them. (it would be something like #define INVOKE_LOG(LogInst, Msg) LogInst.WriteLine(Msg, MethodBase.GetCurrentMethod()))

API Proposal

namespace System.Runtime.CompilerServices
{
    [AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
    public sealed class CallerTypeAttribute : Attribute
    {
        public CallerTypeAttribute();
    }
}

API Usage

static class Utilities
{

    void WriteToStdout([CallerType] Type currentType = default, [CallerMemberName] string memberName = default)
    {
        Console.WriteLine("[{0}::{1}]: invoked!", currentType.Name, memberName);
    }

}

The value of currentType will be filled with typeof(MyType) (or typeof(Utilities) in API usage example).

Alternative Designs

No response

Risks

No response

@nathan130200 nathan130200 added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label May 27, 2024
@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label May 27, 2024
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label May 27, 2024
@jkotas
Copy link
Member

jkotas commented May 27, 2024

Thank you for filling this suggestion. This is request for a new C# language feature. New language features are tracked in https://github.com/dotnet/csharplang repo. The attribute to support the C# language feature is secondary. We add such attributes to dotnet/runtime only once the language feature design is finalized.

This is duplicate of dotnet/csharplang#4984 that proposes a more generalized version of this proposal. Feel free to comment with your specific scenario on that proposal.

cc @jaredpar

@jkotas jkotas closed this as completed May 27, 2024
@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label May 27, 2024
@jkotas jkotas added area-System.Runtime and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels May 27, 2024
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-runtime
See info in area-owners.md if you want to be subscribed.

@github-actions github-actions bot locked and limited conversation to collaborators Jun 27, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Runtime
Projects
None yet
Development

No branches or pull requests

2 participants