I'm writing more library code and came across an annoyance - here is an example
public static TParam RequiredParam<TParam>(TParam param, string name) where TParam : class
{
return param ?? throw new ArgumentNullException(name);
}
private readonly int _testInt;
public TestClass(int testInt)
{
_testInt = RequiredParam(testInt, nameof(testInt));
}
Basically I have to pass in the name in the usage
What I want to have is:
public static TParam RequiredParam<TParam>(TParam param) where TParam : class
{
return param ?? throw new ArgumentNullException(*NAME STILL GETS HERE SOMEHOW*);
}
private readonly int _testInt;
public TestClass(int testInt)
{
_testInt = RequiredParam(testInt);
}
Is there a better way? Back in the days of WPF I used to use [CallerMemberName] like so:
private void PropertyChanged([CallerMemberName]string name = default)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
Does anyone know something similar to achieve the same result for this? Considering how often this library is called I don't mind stepping into post-build steps or whatever, but would prefer to avoid reflection for performance reasons.
Edit - Made my objective clearer