Skip to content

Proposal: Expand supported Caller Info Attributes #351

@HaloFour

Description

@HaloFour

Problem:

The currently supported caller info attributes can only provide the name of the method, the name of the source file and the line number of the call in the source file. This is fine for scenarios such as simplifying implementations of INotifyPropertyChanged. However, if using these attributed for logging the amount of information available is quite limited.

Solution:

Expand the number of supported caller info attributes to allow embedding additional diagnostic information. The following list is quite expansive to discuss/argue over the potential possibilities.

CallerColumnNumberAttribute: The column number of where the method is invoked.
CallerTypeNameAttribute: The simple name of the declaring type of the calling method.
CallerNamespaceAttribute: The namespace of the declaring type of the calling method.
CallerFullTypeNameAttribute: The full name of the declaring type of the calling method.
CallerTypeAttribute: The declaring type of the calling method. This is replaced by the compiler by ldtoken of the type followed by a call to Type.GetTypeFromHandle.
CallerMethodAttribute: The MethodBase of the calling method. This is replaced by the compiler by ldtoken of the method reference followed by a call to MethodBase.GetMethodFromHandle.

Example usage:

using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;

namespace Project1 {
    public static class Program {
        private static void Foo([CallerMemberName] string memberName = null,
                                [CallerTypeName] string typeName = null,
                                [CallerNamespaceName] string namespaceName = null,
                                [CallerFullTypeName] string fullTypeName = null,
                                [CallerFilePath] string filePath = null,
                                [CallerLineNumber] int lineNumber = 0,
                                [CallerColumnNumber] int columnNumber = 0,
                                [CallerType] Type type,
                                [CallerMethod] MethodBase method)
        {
            Debug.Assert(memberName == "Main");
            Debug.Assert(typeName == "Program");
            Debug.Assert(namespaceName == "Project1");
            Debug.Assert(fullTypeName == "Project1.Program");
            Debug.Assert(filePath == "c:\\foo\\bar\\Program.cs");
            Debug.Assert(lineNumber == 29);
            Debug.Assert(columnNumber == 12);
            Debug.Assert(type == typeof(Program));
            Debug.Assert(method == typeof(Program).GetMethod("Main"));
        }

        public static void Main() {
            Foo();
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions