7

Basically what I would like to know is if there is any way to add new 'statements' to the .net based languages? An example of what I'm looking for would be something like introducing a public class MyClass decoratorOf ClassWithLotsOfMethods and in pre-compile time changing that to be a normal class that overrides everything by default but the methods I define.

Another example would be the .net 3.5 auto properties, or extension methods, etc

This is just for fun, not that I really want to do this, just curious if is possible

Thanks! Seba

Sebastian Piu
  • 7,838
  • 1
  • 32
  • 50
  • Sounds like you're thinking of the way I'd like extension methods to work--if Foo is declared as an ExtensionClass of Bar, then an object declared as a Foo would be considered to be a Bar, except for compile-time processing of self-static methods and properties of Foo which would unconditionally shadow those of Bar (even when corresponding methods exist in Bar). Such a design would allow members to be added to Bar without breaking the code that uses Foo, even if such members match the names of Foo's extensions. Too bad I don't know any way to make the language support that. – supercat Oct 19 '10 at 14:59
  • @supercat: you should take a look at [Scala implicits](http://www.artima.com/weblogs/viewpost.jsp?thread=179766) – Jordão Oct 19 '10 at 15:40

4 Answers4

8

C# doesn't allow this. You can of course tweak the generated IL with a post-compiler (like CciSharp).

Some alternative .NET languages that allow extensions are Nemerle and Boo.

Jordão
  • 55,340
  • 13
  • 112
  • 144
4

There is nothing built-in.

You could of course use a PreProcessor but that won't make you popular.

H H
  • 263,252
  • 30
  • 330
  • 514
  • 1
    Yeah, look at Simula -- all they did was invent object-oriented programming. Or C++ -- that's not very popular, is it? :-) – Ken Oct 19 '10 at 14:51
  • @Ken: how would adding a pre-processor make him popular? And what does this have to do with Simula (which was not actually an extension of ALGOL)? – John Saunders Oct 19 '10 at 19:23
  • I see no reason to believe the use of a preprocessor would have any particular effect on one's popularity, as there are good examples of extreme popularity and unpopularity both with and without one. And (if a dozen separate histories I found on the internet are to be believed) Simula started life as a preprocessor for Algol (as C++ started life as a preprocessor for C), though (like C++) it shortly took on a life of its own. – Ken Oct 20 '10 at 05:14
4

Not that I know about, but take a look at PostSharp and T4 Templates and see if that can solve your problem :)

Onkelborg
  • 3,927
  • 1
  • 19
  • 22
1

Related: Extending the Mono C# compiler: is there any documentation or precedent?

Community
  • 1
  • 1
Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653