Skip to main content

Antonio Cisternino's Home Page

Go Search
Antonio Cisternino's Home Page > My Blog > [a]C#  

My Blog: [a]C#




Time for a new post guys!

Today I would like to introduce to [a]C#: a little extension to C# language we have implemented recently.

I'm not sure that people really understands how deep and wide is spreading the use of custom attributes. Here are but fews:

  • When you write a control for Visual Studio you use the annotations to indicate properties, design time visualization and many other aspects.
  • ASP.NET parser behavior (for instance for templated controls) is controlled with custom attributes.
  • Serialization classes and transient fields are annotated with attributes.
  • Indigo, the next generation of remoting in Longhorn, make substantial use of custom attributes to control, in a declarative way, communication of controls.
  • Web Service interface to methods is controlled by custom attributes.

The idea of having an extensible model for reflection is so good that even Java Tiger (the upcoming v1.5) includes an analogous notion (though with the different name of annotations).

Although really flexible and customizable the annotation model has a lack: you can only label assembly and class elements (i.e. fields, classes/structs, methods, arguments, ...). In fact you can annotate all the elements that are exposed through reflection.

We found plenty of applications that could benefit from annotations of code blocks. I oft find C# source code that annotates code blocks using #region/#endregion: it would be interesting (and wise) to carry the region information at runtime, so that program and tools may retrieve this information at runtime.

A First Example

Let start with a bit of [a]C# code:

class Example {
  void Foo() {
    Console.WriteLine("This code is not annotated");
    [MyAnnotation] {
      Console.WriteLine("Code inside MyAnnotation");
      [NestedAnnotation] {
        Console.WriteLine("Code inside two annotations");
      [Another] {
        // Some code here

As you see the only extenstion to language's syntax is the ability of putting custom attributes inside method bodies. A code annotation (as we call them) can be prefixed to single statements or blocks.

Code attributes are simply classes that inherits from ACS.CodeAttribute rather than System.Attribute. Apart from this difference code attributes are like custom ones (in fact we reduce code annotations to custom attributes plus something else).

Annotations are ignored if you run a method, but you can use a function of [a]C# runtime that is called (this is a new name!) GetCustomAttributes(m), where m is the MethodInfo of the method we are interested to inspect.

Apart from its introspection capabilities the runtime of [a]C# will provide functions to extract, replace or inject code annotates: at runtime you'll be able to manipulate binary assemblies with high level operations on code blocks. At the moment the interface for code generation provides faciities to fill method bodies using Reflection.Emit. But this part of the language is still under development.

Possible Applications

It is always good to retain information about a program: sooner or later it would turn useful. Few examples of applications that can benefit from this extension are:

  • Hints about how to parallelize a method body (to the JIT?)
  • Assertions, code properties
  • Aspect Weaving at runtime

If you are interested in [a]C# you can download it from as part of the CodeBricks library.

Look at the ACS library (and the solution within it).

In future posts I would like to describe how we implemented the compiler and how we encode scope information about annotations into standard CLI binaries.




Created at 4/7/2004 23:27  by Antonio Cisternino 
Last modified at 4/8/2004 0:12  by Antonio Cisternino