Conditional Attribute Classes

1. How to avoid bloating IL code using the conditional attribute classes?

Example 1:

#define TEST

 

using System;

using System.Diagnostics;

 

namespace ConditionalAttributes

{

   

    [Conditional("TEST")]

    [Conditional("VERIFY")]

    public class CondAttribute : Attribute { }

 

    [Cond]

    class Program

    {

        static void Main(string[] args)

        {

            Trace.WriteLine("Is Cond defined " + Attribute.IsDefined(typeof(Program), typeof(CondAttribute)));

            Console.ReadKey();

        }

    }

}

 

Reflection:

[Cond]
internal class Program
{
    // Methods
    public Program();
    private static void Main(string[] args);

}

 

Output:

Is Cond defined True

 

Example 2:

//#define TEST

 

Reflection:

internal class Program
{
    // Methods
    private static void Main(string[] args)
    {
        Trace.WriteLine("Is Cond defined " + Attribute.IsDefined(typeof(Program), typeof(CondAttribute)));
        Console.ReadKey();
    }

}

 

Output:

Is Cond defined False

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s