Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to generate code in C# similar to using decorators?

Tags:

c#

Say I have a pattern that I'm constantly repeating. Something like:

static class C {
  [DllImport("mydll")]
  private static extern uint MyNativeCall1(Action a);
  public static uint MyWrapper1(Action a) {
    // Do something
    return MyNativeCall1(a);
  }

  [DllImport("mydll")]
  private static extern uint MyNativeCall2(Action a);
  public static uint MyWrapper2(Action a) {
    // Do something
    return MyNativeCall2(a);
  }

  //...

  [DllImport("mydll")]
  private static extern uint MyNativeCallN(Action a);
  public static uint MyWrapperN(Action a) {
    // Do something
    return MyNativeCallN(a);
  }
}

The only thing different in all those is the name of the native function and wrapper method. Is there a way to generate them via something like decorators? At first I thought C# attributes were decorators. That is, that I could generate the code via something like [GenerateScaffolding("MyNativeCall1")]. But it seems attributes are more like annotations, instantiating a class that holds some metadata.

Neither does C# have macros. So is there any way to do this?

A few things to keep in mind:

  • The idea is that the wrapper methods have extra code; they are not merely calling the native functions.
  • The idea is also that the generated code can be interleaved with other existing code inside a class and not to generate the class file itself; something like either decorators or C/C++ macros.
  • The approach should not depend on any particular IDE. Specifically, I'm not on Visual Studio.
like image 307
SaldaVonSchwartz Avatar asked Jun 25 '14 07:06

SaldaVonSchwartz


People also ask

Can I write C code in Matlab?

In MATLAB®, you can extend your C and C++ code with a MEX function and call it like any MATLAB built-in function. That means you can use existing C and C++ code without rewriting your algorithms in MATLAB. MEX functions enable C and C++ code to create and modify MATLAB arrays in the MATLAB workspace.

What is source code generator?

A Source Generator is a piece of code that runs during compilation and can inspect your program to produce additional files that are compiled together with the rest of your code.

What do I need to start writing C code?

Before anything else, you are going to need a text editor. Each operating system has its own editors already installed (Windows has Notepad, Linux distributions have Kwrite etc), but consider getting another. Notepad++ is an excellent editor you can use (and not only to write C code).

How to generate a number in range in C?

As C does not have an inbuilt function for generating a number in the range, but it does have rand function which generate a random number from 0 to RAND_MAX. With the help of rand () a number in range can be generated as num = (rand () % (upper – lower + 1)) + lower

How to make the best use of C programming language?

Since the C programming language offers a lot of features, it is up to us if we make the best use of it or not. 1. Follow the latest rules in the C Standard compiler documentation rigorously. For instance, according to the latest C standards, it is mandatory to use the int data type before the main function and with return 0.

How do I execute a C program using GCC?

In order to execute your C programs, you are going to need a compiler like GCC. Let's say that you have a file named test.c, which you want to compile. Go to the directory of that file and type gcc test.c -o test gcc: the command that orders GCC to compile your code. test.c: the name of the file you want compiled. -o: a flag that stands for output.


Video Answer


1 Answers

Taking the idea from this MSDN article on T4 Templates, you could so something like:

<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ output extension=".cs" #>
static class C {
<#  
    int N = 15;
    for(int i=0; i<N; i++)
    { #>
    [DllImport("mydll")]
    private static extern uint MyNativeCall<#= i #>(Action a);
    public static uint MyWrapper<%#= i #>(Action a) {
        return MyNativeCall<#= i #>(a);
    }
<# }  #>
}
like image 86
CodingIntrigue Avatar answered Oct 08 '22 22:10

CodingIntrigue