none
c# compiler extension - analyse c# file and add code

    Question

  • Hello,

    the Problem is very simple:

    I write properties in a c# file.

    for each property there must be a special field in the class with a special type which is used in the property.

    I would like to write the Code

    string MyProp

    {

        set

       {

          myProp = value;

       }

      get

      {

          return myProp;

      }

    }

    many such properties ......

    .... more c# Code ....

    More I do not want to write in the properties.

    Now i would like to have compilier Extension I could write Code so that the Compiler sees this properties and add for each

    property a field in the class an a Funktion-call in the set of the property as follows:

    --- fields to generate---

    MyObjType  specialObj_myProp01 = new MyObjType(this);

    MyObjType  specialObj_myProp02 = new MyObjType(this);

    ... for each property.

    in set of the Propery MyProp01 must be generated:

    ..

    set

    {

        myProp01 = value;

       //this must be generated:

       specialObj_myProp01.CallFu(value);

    }

    this for all properties.

    The question is: is there a possibilty to write Code which the c# Compiler extends, so that the existing c# code is analysed and for each property the code as described is generated before final compile the c# file (the code for Analyse and generate I would write, but the Compiler must use it).

    I know a first thinking is to use T4 - but in T4 I must write the c# code as plain text and not as c# file and so I cannot see how to Analyse the c# file which is not there.

    Thank you for any help.

    Rowe10

    Thursday, April 20, 2017 5:29 AM

Answers

  • Hello,

    thank you for all your help.

    I think best is to invest in Roslyn and use it to do the work.

    Thank you

    Rowe

    • Marked as answer by rowe10 Friday, April 21, 2017 9:28 PM
    Friday, April 21, 2017 9:28 PM

All replies

  • When the resulting files of T4 templates are source files (like those you see in Entity Framework, the folded items under the .edmx file), why it cannot be analyzed?
    Thursday, April 20, 2017 5:38 AM
    Answerer
  • Maybe write a separate command-line tool, which scans the text of .cs files (using Regular Expressions, for example) and makes the modifications if needed. This tool will be defined in the Build Events area of Project Properties.



    • Edited by Viorel_MVP Thursday, April 20, 2017 6:08 AM
    Thursday, April 20, 2017 6:06 AM
  • I think you'll want to look into writing a VS extension that can use Roslyn to analyze the source code. Such a discussion is too large for the forums but you can read up on how to write an extension in MSDN.
    Thursday, April 20, 2017 2:00 PM
    Moderator
  • Hello,

    thank you for all your help.

    I think best is to invest in Roslyn and use it to do the work.

    Thank you

    Rowe

    • Marked as answer by rowe10 Friday, April 21, 2017 9:28 PM
    Friday, April 21, 2017 9:28 PM