I understand why macros were not included in the language; that they can easily become abused. However I now and again stumble upon situations where they seem desirable. Just recently I was writing a method to log image files:
public void LogImage(Bitmap image, string name)
{
image.Save(LogRootDirectory + name + ".bmp");
}
Subsequently, I thought it clever to utilize the nameof() feature to better provide information about the image I am logging. In several places in my code I have lines very similar to this:
ImageLogger?.LogImage(processedImage, nameof(processedImage));
What bothers me is the repetitiveness of this and I would much prefer a pre-processor command similar to this:
#define LOGIMAGE(img) ImageLogger?.LogImage(img, nameof(img));
Is there a way to emulate the above macro behaviour to better streamline my code?
Note: This question differs in that the author is asking if per-processor definitions exist in C#. An assertion that I made clear from the beginning that I know that they do not. My question is in reference to techniques that I may use in substitution. Which I believe was provided to me in the marked answer.