Introduction
Annotated C# ([a]C#) is a small extension to the C# language. The language allows to annotate code inside methods with custom attributes. A program at runtime can rely on the runtime of the language for retrieving the annotations from assembly metadata. Runtime also support code manipulation of annotated code fragments.
Notes
[a]C# still implements the C# 1.0 syntax. We are working to implement the new syntax of version 2.0.
Why is in Robotics4.NET?
The language was designed to allow code annotation. We believe that reflection is a useful tool in robot programming. In particular devices (perhaps roblets) may declare what they do by simply annotating the code using custom attributes that indicates a coarse grain semantics of the code. This approach is being referred as "Attribute Oriented Programming" and is supported by other tools out there (for instance X-Doclet ). Code manipulation can also be useful in programming software capable to adapt the behavior to the environment.
Example
The following code shows an example of code annotation. It shows how we can encode semantics information about code, in this case we give hints about how to execute in parallel the body of a method.
Show
using System;
using ParallelAnnotations;
namespace ParallelizableCode {
/// <summary>
/// Summary description for Main.
/// </summary>
public class ParallelizableCode {
public static void Main(string[] args) {
Console.WriteLine("Parallelizable code sample");
[Parallel("Begin of a parallelizable block")] {
Console.WriteLine("This is performed by the main thread");
[Process("First process")] {
// Computation here
}
[Process("Second process")] {
// Computation here
}
}
Console.WriteLine("Recombination code here");
}
}
}
The following code shows how easy is to retrieve the information associated with a method.
Show
using System;
using System.Reflection;
using ACS;
public class InspectMain {
public static void DumpTree(AnnotationTree t, string indent) {
foreach (CodeAttribute c in t.Node)
Console.WriteLine(indent + c.ToString());
foreach (AnnotationTree at in t)
DumpTree(at, indent + " ");
}
public static void Main(string[] args) {
if (args.Length == 0) {
Console.WriteLine("Usage: InspectMain [-r] <assembly with Main>");
return;
}
bool close = false;
string an = null;
if (args[0] == "-r") {
close = true;
if (args.Length < 2) {
Console.WriteLine("Usage: InspectMain [-r] <assembly with Main>");
return;
}
an = args[1];
} else {
an = args[0];
}
Assembly a = Assembly.LoadFile(an);
Type[] types = a.GetTypes();
MethodInfo m = null;
int i = 0;
while (m == null)
m = types[i++].GetMethod("Main", BindingFlags.Static | BindingFlags.Public);
AnnotationTree[] t = Annotation.GetCustomAttributes(m, close);
foreach (AnnotationTree at in t)
DumpTree(at, "");
}
}