C # Conditional attribute trap

less than 1 minute read

Problem overview

I defined define and used Conditional, but why isn’t the function called? ?? ??
I encountered an incident such as.

Like this

#define Debug_AAA
public class AAA
{
	public AAA()
	{
		new BBB().Method_DebugAAA();
		new BBB().Method_DebugBBB();
	}
}
#define Debug_BBB

using System.Diagnostics;

public class BBB
{
	[Conditional ("Debug_AAA")]
	public void Method_DebugAAA()
	{
		UnityEngine.Debug.Log("If you call from AAA, you will get a log");
	}

	[Conditional ("Debug_BBB")]
	public void Method_DebugBBB()
	{
		UnityEngine.Debug.Log("When I call it from AAA, there is no log");
	}
}

(Since I was working with Unity, I’m sorry that the log output is Unity specification)

What is it?

Apparently, the Conditional attribute refers to the caller’s define, not the define in the script.
(Impressions of people who do not read the reference)
In the first place, it may not be good to set define in each script ^^;

Finally

Define define outside (for example, Unity’s ScriptingDefineSymbols)
I’m in trouble when I want to use it as below … I feel …

#if Debug_AAA || Debug_BBB
#define ORIGINAL_DEBUG
#endif

[Conditional ("ORIGINAL_DEBUG")]
public void OriginalDebugMethod(){}

For the time being, I will organize define in my project.
In some cases, you can use #if obediently.

Tags:

Updated: