none
How to make my own Conditional Attribute, but more handy and useful RRS feed

  • Question

  • How to make attribute identical to #if/#endif usage:

    class Class

    {

    #if SERVER

    int X;

    #endif

        int Y;

    #if SERVER

        void ServerFunction(){}

    #endif

        void ClientFunction(){}

    }

    class Class

    {

        [ServerOnly]

        int X;

        int Y;

        [ServerOnly]

        void ServerFunction(){}

        void ClientFunction(){}

    }

    I need to get result code without any footprints of [ServerOnly] variables and functions (even inside Reflector):

    class Class

    {

        int Y;

        void ClientFunction(){}

    }

    And i need compiler to get compile time errors when i use this functions or variables in Client code, identically like when i use #if/#endif.


    • Edited by NewbTom Friday, May 4, 2012 5:02 AM
    Friday, May 4, 2012 5:01 AM

Answers

  • "Am i right ? is it normal way ?"

    Well, what you are trying to do seems a bit unusual so debating if this is "normal" or not doesn't make much sense. Recompiling the assembly seems like a reasonable approach given your requirement. The only trouble is that it is not exactly easy to do.

    Roslyn may help you do this at the source level, read the source, analyze it and write new source that can be compiled. As I said before I kind of doubt that it will be possible to do such stuff on the fly by modifying the compiler behavior. But who knows, Roslyn is not even in beta at the moment.

    • Marked as answer by NewbTom Friday, May 4, 2012 8:14 AM
    Friday, May 4, 2012 7:42 AM
    Moderator

All replies

  • That's not possible. Such an attribute requires compiler support, you can't create your own attribute and expect the compiler to know about it and handle it specially.
    Friday, May 4, 2012 6:06 AM
    Moderator
  • Why it's not possible ? will it be possible in future version of Visual Studio ? for example with Roslyn ?
    Friday, May 4, 2012 7:07 AM
  • "Why it's not possible?"

    Because the compiler would need to understand the meaning of your custom attribute. That can happen without making changes to the compiler itself.

    "for example with Roslyn ?"

    I doubt that. Roslyn could allow you to create some code analyzer that helps you find calls to method marked with such a custom attribute. But I doubt very much it would allow you to change the way the compiler works. Why would it do that when such a feature is already available in the form of #if/#endif

    Friday, May 4, 2012 7:23 AM
    Moderator
  • Well actually that example as i mentioned is only small part of what i need, but without even this, other things impossible too.

    In short: I need ability to change code programmatically before compiler begin to compile it.

    Now for example i can reach this behaviour with following:

    1) Compile my code in Visual Studio, with my attributes

    2) Recompile resulting assembly programmatically with handmade C# utility using reflection, emitter etc.., and analizing my attributes, and cutting out unneeded code or insert new code, change code...

    Am i right ? is it normal way ?

    If only i would only need #if/#endif, i would use it, but i also need RPC (like in WCF) and possibly other staff.

    Compiler will do usual work, rules exactly the same, but only Source code need to be changed before compile.


    • Edited by NewbTom Friday, May 4, 2012 7:36 AM
    Friday, May 4, 2012 7:33 AM
  • "Am i right ? is it normal way ?"

    Well, what you are trying to do seems a bit unusual so debating if this is "normal" or not doesn't make much sense. Recompiling the assembly seems like a reasonable approach given your requirement. The only trouble is that it is not exactly easy to do.

    Roslyn may help you do this at the source level, read the source, analyze it and write new source that can be compiled. As I said before I kind of doubt that it will be possible to do such stuff on the fly by modifying the compiler behavior. But who knows, Roslyn is not even in beta at the moment.

    • Marked as answer by NewbTom Friday, May 4, 2012 8:14 AM
    Friday, May 4, 2012 7:42 AM
    Moderator