You are on page 1of 5

4/4/2015

Amazedsaint's Tech Journal: 3 Gems in Mono for .NET Programmers The hidden potential of Mono.CSharp, Mono.Cecil And Mono.TextTemplating

Amazedsaint's Tech Journal

search

Anoop's observations on tech, web, design & architecture, .net and more..

Home

C#

.NET

Web

Javascript

Bigdata

Architecture

3 Gems in Mono for .NET Programmers The hidden


potential of Mono.CSharp, Mono.Cecil And
Mono.TextTemplating

Archive

Subscribe

Enter your mail to subscribe


and get a free copy of my
"Revisiting C#" and
"Applying Design
Patterns" Ebooks

ANOOP MADHUSUDANAN
Follow @am azedsaint

submit

Tw eet

Subscribe

Saturday, October 16, 2010 | Tags: .NET, CSharp, Mono

Mono is getting more mature. I should say, Mono guys are even

Feeds

outshining their Microsoft counterparts in some areas. For example,


Monos C# compiler already supports Compiler as a Service. In this
post, well have a quick look at some interesting Mono libraries that
you can use in your .NET projects, to take advantage of some of
their functionalities, that is not present in the .NET stack.
1 - Mono.CSharp For Compiler as a Service and C# as a

Like In Facebook

scripting language
Anders in his C# Futures Talk mentioned about C# Compiler as a

Amazedsaint's
.NET Journal

Service and demonstrated an REPL (Read Evaluation Print Loop)

Like 471

implementation. And few months back, I got pretty surprised when I found that Mono announced Compiler
as a Service support. This will enable you to evaluate C# code on the fly, and more importantly, to use C#
as a scripting language in your applications. Here is a step by step guide to this feature.
Hosting Monos C# Compiler As a Service in .NET Apps
Dynamic Filtering and Querying in .NET Applications
Using C# as a scripting language
2 - Mono.TextTemplating For T4 (Text Templating) Functionality in your .NET applications
In a lot of scenarios, a custom text template processor comes in handy. I remember putting together a
simple, custom Text template processor some time back for generating Emails. With in Visual Studio, there
is a Text Templating transformation engine built in, better known T4. It is used for a lot of scenarios, mainly
for generating code from Models, and is used in various VS Packages for code generation purposes. You
can also write your own Template files (*.tt) with in Visual Studio, and Clarius has a cool T4 Editor available

About Anoop

for Visual Studio.


All is good. But what if you want to implement some templating functionality in your own applications? We
cant use the Microsoft.VisualStudio.TextTemplating libraries, because I don't believe that T4 can be
legally redistributed without Visual Studio. But you can rely on Mono equivalent T4
implementation,Mono.TextTemplating
And, if you want to see an example, you should check out a Custom View Engine I developed for ASP.NET

http://www.amazedsaint.com/2010/10/monocsharp-monocecil-and.html

1/5

4/4/2015

Amazedsaint's Tech Journal: 3 Gems in Mono for .NET Programmers The hidden potential of Mono.CSharp, Mono.Cecil And Mono.TextTemplating

MVC using Mono.TextTemplating and the related example code. More than an ASP.NET MVC View Engine
example, it is also an example of how to host the T4 Engine in your own applications.
3 - Mono.Cecil For Modifying .NET Assemblies, And a better alternative to .NET Reflection
If you use .NET Reflection heavily, you should have a look at Mono.Cecil. You can also use Mono.Cecil to
modify a compiled assembly, or to weave some code in to your assemblies (AOP) directly. To try out Cecil,
get the latest version 0.9 from Github and compile Mono.Cecil library in VS2010.
Now, create a console application in Visual Studio, and try this example after adding a reference to
Mono.Cecil.dll.

Architect, Developer, Speaker |


Innovation Agent | Microsoft MVP in

using System;

C# | Posts on JavaScript, Mobile, C#,

using System.Collections.Generic;

.NET, Cloud. Alchemist in the making.

using System.Linq;

Seeker.

using System.Text;
using Mono.Cecil;
FOLLOW =>

namespace CecilAppTest

Twitter | Facebook | Linked In

{
class Program
{
static void Main(string[] args)

Featured

{
//Open a given assembly. Let us assume the
//first command line param is the path to the assembly to load and inspect
AssemblyDefinition assemblyDefinition = AssemblyDefinition.ReadAssembly(args[0]);

//All Modules in this assembly


foreach (var modDef in assemblyDefinition.Modules)
{
Console.WriteLine(" +--Module {0}", modDef.Name);

Checkout Big Data for

//All types in this module

.NET Programmers

foreach (var typeDef in modDef.Types)


{
Console.WriteLine("

+--Type {0}", typeDef.Name);

Recently Popular

//All methods in this type


foreach (var methodDef in typeDef.Methods)
{
Console.WriteLine("

--Method {0}", methodDef.Name);

}
//All properties in this type
foreach (var propDef in typeDef.Properties)
{
Console.WriteLine("

--Property {0}", propDef.Name);

}
}
}
}

Top 7 Coding
Standards &
Guideline
Documents For
C#/.NET Developers
Some time back, I collated a list
of 7 Must Read, Free EBooks for
.NET Developers , and a lot of
people found it useful. So, I
thought about...

}
}

Hack
Raspberry Pi

Compile the above example, and try it against a .NET dll to see the output, by providing the target
assemblys path
Share

The above example shows how to iterate through all modules, types, methods and properties in an
assembly using Mono.Cecil. What is more interesting is, you can even modify the information and even
save it back. Paul has a good post on using Cecil to obfuscate your .NET assemblies. Also, have a look at
his NCloak obfusctor, it is open source, that is built leveraging Cecils power.

How To Build
Apps In C#,
WinForms and ASP.NET
Using Mono In Pi
Recently I was doing a bit of R&D
related to finding a viable, low
cost platform for client nodes.

Also, check out my C# AccessPrivateWrapper where I explore how to use reflection to access private

Obviously, I came across

classes, variables etc.

http://www.amazedsaint.com/2010/10/monocsharp-monocecil-and.html

2/5

4/4/2015

Amazedsaint's Tech Journal: 3 Gems in Mono for .NET Programmers The hidden potential of Mono.CSharp, Mono.Cecil And Mono.TextTemplating
Raspberry Pi...
Like

Share 471 people like this. Be the first of your friends.

Analyzing some
Big Data Using
C#, Azure And
Apache
Hadoop A Stack Overflow
.NET Namespace Popularity
Finder

0 Comments
Recommend

Amazedsaint's .Net Journal

Share

Login

Sort by Oldest

Time to do something
meaningful with C#, Azure and
Apache Hadoop. In this post,
well explore how to create a

Start the discussion

Mapper and Reducer in C#, to a...

MVVM - Binding Multiple


Radio Buttons To a single
Enum Property in WPF

Be the first to comment.

I had a property in my View


Model, of an Enum type, and
wanted to bind multiple radio

Subscribe

Add Disqus to your site

buttons to this. Firstly, I wrote a


Privacy

simple Enum to B...

Changing
Times For Web
Newer Post

Older Post

Developers 6
Tips You
Should Read To Survive
The context of this post is about
the changing times for a web
developer, and I see a lot of web
developers still lagging behind
especiall...

An
Asynchronous
Delegate
Command for
your WPF MVVM Apps AsyncDelegateCommand
This post assumes you are
familiar with basic MVVM
concepts and
RelayCommand/DelegateCommand
In a lot of scenarios, you need to
perform o...

Type Script
Language
Features
Classes,
Inheritance & Overriding
Type Script brings a lot more
structure and object oriented
goodness to JavaScript, thought
under the hood it is just a Syntax
sugar for Jav...

http://www.amazedsaint.com/2010/10/monocsharp-monocecil-and.html

3/5

4/4/2015

Amazedsaint's Tech Journal: 3 Gems in Mono for .NET Programmers The hidden potential of Mono.CSharp, Mono.Cecil And Mono.TextTemplating
5 Awesome
Learning
Resources For
Programmers
(To help you and your kids
to grow the geek neurons)
Happy New Year, this is my first
post in 2012. Ill be sharing few
awesome learning resources Ive
bookmarked, and will be pointing
out some...

7 Freely
available EBooks/Guides I
found essential
for .NET Programmers and
Architects
A lot of people ask me for good
book recommendations. Hence,
for some time, I was thinking
about compiling a quick list of
useful free E-Boo...

CakeRobot - A
Gesture Driven
Robot That
Follows Your
Hand Movements Using
Arduino, C# and Kinect
Over the last few weekends Ive
spend some time building a
simple robot that can be
controlled using Kinect. You can
see it in action below....

12

Code & Hacks

Share

ElasticObject
SignalWire
Kibloc/Kinect
Ksigdo
Vingy

http://www.amazedsaint.com/2010/10/monocsharp-monocecil-and.html

Must Read
7 Freely Available Ebooks For .NET
developers

Total Pageviews

3,097,981

5 Back to Basics C# Articles Fluent Interfaces, Expr Trees etc


3 Gems from Mono to spice up your
.NET Apps

4/5

4/4/2015

Amazedsaint's Tech Journal: 3 Gems in Mono for .NET Programmers The hidden potential of Mono.CSharp, Mono.Cecil And Mono.TextTemplating
Top 5 Common Mistakes .NET
Developers Must Avoid
6 Cool VS2010 Tips you may find
interesting
4 .NET 4.0 Libraries you *should*
know about
Translate

Select Language

2012. All Rights Reserved. Amazedsaint.com

http://www.amazedsaint.com/2010/10/monocsharp-monocecil-and.html

Powered by Blogger.

5/5

You might also like