0

My current code situation is that I have in assembly A the following code:

public class Foo
{
    public Foo()
    {
        CreateDebugMessage();
    }

    [Conditional("DEBUG")]
    [DebuggerStepThrough]
    private void CreateDebugMessage()
    {
        AddMessageType(MessageType.Debug, "Debug",
            "/Company.App.Class;component/Images/image.png", Brushes.Green, false);
    }
}

Some extra information is that I am using MEF and this method is called from the constructor. I have an assembly B (where I am importing assembly A) which depending on whether I am on DEBUG or RELEASE mode I want the Debug message to be created when I instantiate the class:

var foo = new Foo();

If I am on Debug mode I want the debug message to be created. If I am on Release mode I DO NOT want the debug message to be created.

I thought the Conditional attribute would be better than the #iF DEBUG statement. This question showed me how WRONG I was! Since on runtime the method is never reached.

At this point I understand that the "#iF Debug" and "[Conditional("DEBUG")]" statements won't cut it for what I want to achieve.

Therefore my question is, how to make this scenario work?

Community
  • 1
  • 1
El Padrino
  • 1,141
  • 1
  • 11
  • 26
  • 4
    what scenario? What do you want to happen? Should it run or should it not? – default Jan 09 '15 at 13:51
  • The question you've linked already has an example, why do you need the new one? – VMAtm Jan 09 '15 at 13:52
  • @Default I want to run the code in the CreateDebugMessage from my code in assembly B. The problem I have is that it is never reached due to the conditional attribute. Nevertheless, I need that method to be run only when assembly B is in DEBUG. – El Padrino Jan 09 '15 at 13:57
  • @VMAtm I'm giving an example trying to explain what I'm trying to do. I found that the #iF DEBUG and Conditional attribute might not be the right way forward. Don't know where to go from here. – El Padrino Jan 09 '15 at 13:59

1 Answers1

3

The attribute works as it should, see Conditional Compilation in Referenced Assemblies. The attribute depends on the compilation symbols of the calling assembly. I tested and confirmed this: a method in an assembly with [Conditional("DEBUG")], compiled on Release, will only get called if the calling assembly is compiled in Debug. If this isn't the case for you, your code does not match your description.

The relevant part in your question is of course "This method is called from the constructor.". The attribute works for the direct caller, which in your case is the constructor of the containing class, which is Release.

You'll have to make it public and explicitly call the method:

public class Foo
{
    public Foo()
    {           
    }

    [Conditional("DEBUG")]
    [DebuggerStepThrough]
    public void CreateDebugMessage()
    {
        AddMessageType(MessageType.Debug, "Debug",
            "/Company.App.Class;component/Images/image.png", Brushes.Green, false);
    }
}

var foo = new Foo();
foo.CreateDebugMessage();
Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272