Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a delegate when there is a conditional attribute

I have a Portable Class Library with a class PCLDebug:

public static class PCLDebug {
    public static Action<string> LogLine { get; set; }
}

What I want to do is set things up once in the outer project, then be able to call LogLine within the PCL to print stuff to the VS Output window. Here is my attempt:

MyPCL.PCLDebug.LogLine = System.Diagnostics.Debug.WriteLine;

The problem here is that the compiler complains, because System.Diagnostics.Debug.WriteLine has a conditional attribute of Debug:

Cannot create delegate with 'System.Diagnostics.Debug.WriteLine(string)' because it has a Conditional attribute

I am actually fine with it if the LogLine call only works in the debugging environment. But how do I keep the compiler happy?

like image 315
William Jockusch Avatar asked Dec 17 '13 16:12

William Jockusch


2 Answers

You could try wrapping it in a lambda function:

MyPCL.PCLDebug.LogLine = s => { System.Diagnostics.Debug.WriteLine( s ); };
like image 112
Kyle Avatar answered Nov 05 '22 08:11

Kyle


you can use this alternative notation too:

    MyPCL.PCLDebug.LogLine = delegate(string text) { System.Diagnostics.Debug.WriteLine(text); };
like image 5
Rodrigo Castro Eleotério Avatar answered Nov 05 '22 06:11

Rodrigo Castro Eleotério