Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / July 2006

Tip: Looking for answers? Try searching our database.

Debug conditional code in class that implements an interface... does it work?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
hellosticky@gmail.com - 13 Jul 2006 02:49 GMT
I'm creating some logging code, and each one of my classes has an
internal static readonly instance of the logger which various methods
use, e.g.:

interface ILogger
{
 void LogDebug(...);
 void LogInfo(...);
 void LogError(...);
 ...
}

What I did was to create two classes, one in the case where the Logger
for that class is off, and the other when it is on.

class Logger : ILogger {...}

class NullLogger : ILogger {
        [Conditional("DEBUG")]
        public void LogDebug(...)
        {
        }
...
}

Now, my question is, the ILogger instance is assigned at runtime. So,
if I assign the NullLogger at runtime, which has [Conditional("DEBUG")]
will it still for sure not evaluate the parameters passed to the Log
method if it is of type NullLogger?

For example, with Conditional("DEBUG"), the executeLongMethodToGetValue
is not executed.

myLog.LogDebug(myObj.executeLongMethodToGetValue())

In this case, myLog is an interface assigned at runtime. If it is of
type NullLogger, will this still not evaluate
executeLongMethodToGetValue?

Thanks!
Mattias Sjögren - 13 Jul 2006 06:45 GMT
>Now, my question is, the ILogger instance is assigned at runtime. So,
>if I assign the NullLogger at runtime, which has [Conditional("DEBUG")]
>will it still for sure not evaluate the parameters passed to the Log
>method if it is of type NullLogger?

No it will evaluate the parameters. The ConditionalAttribute is a
compile-time feature.

Your code shouldn't even compile. Conditional method are not allowed
to implement interface members.

Mattias

Signature

Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.