In C#, this is not possible. The information which is sure to be available is the following:
- The name of the calling method.
- The name of the file where the call occurs.
- The line number of the call.
Source: Caller Information (C# and Visual Basic)
If you are just trying to simplify your argument checking, create a file in the following folder:
%USERPROFILE%\Documents\Visual Studio 2012\Code Snippets\Visual C#\My Code Snippets
If you have multiple versions of Visual Studio, or a different version than 2012, an appropriately named alternative folder will exist for the other version(s).
Name the file ThrowIfArgumentNull.snippet, with the following content. To use it, type tan
in the editor and press tab twice. You'll get IntelliSense support for typing the argument name, and the string argument will be filled in automatically. This snippet creates a standard ArgumentNullException
, but you can modify it to use a Guard
or Argument
class if that's what your project uses.
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
<Title>Throw if argument null</Title>
<Author>Sam Harwell</Author>
<Description>Throw an ArgumentNullException if the specified argument is null.</Description>
<HelpUrl>
</HelpUrl>
<Shortcut>tan</Shortcut>
</Header>
<Snippet>
<Declarations>
<Literal Editable="true">
<ID>arg</ID>
<ToolTip>arg</ToolTip>
<Default>arg</Default>
<Function>
</Function>
</Literal>
<Literal Editable="false">
<ID>ArgumentNullException</ID>
<ToolTip>ArgumentNullException</ToolTip>
<Default>ArgumentNullException</Default>
<Function>SimpleTypeName(global::System.ArgumentNullException)</Function>
</Literal>
</Declarations>
<Code Language="csharp"><![CDATA[if ($arg$ == null)
throw new $ArgumentNullException$("$arg$");$end$]]></Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>