The code complexity extension for Visual-Studio is now compatible with 2013.
The new version is available at the Visual-Studio Gallery and the source code at CodePlex.
The code complexity extension for Visual-Studio is now compatible with 2013.
The new version is available at the Visual-Studio Gallery and the source code at CodePlex.
In this post we’ll explore what happens behind the scenes when we place code in field initializer compared to constructor body.
We’ll start from a simple example:
public class MyClass
{
private static int index = 1;
public int x = index++;
public int y;
public MyClass()
{
y = index++;
}
}
The result here is very intuitive, this code:
var instance = new MyClass();
Console.WriteLine("x = " + instance.x);
Console.WriteLine("y = " + instance.y);
So far there’s no surprise, the field initializers code is performed prior to code of the explicit constructor.
Let’s observe the next code:
public class Base
{
protected static int index = 1;
public int w = index++;
public int x;
public Base()
{
x = index++;
}
}
public class Derived : Base
{
public int y = index++;
public int z;
public Derived()
{
z = index++;
}
}
There are two outputs which one may expect:
What actually happens?
var instance = new Derived();
Console.WriteLine("Base.w = " + instance.w);
Console.WriteLine("Base.x = " + instance.x);
Console.WriteLine("Derived.y = " + instance.y);
Console.WriteLine("Derived.z = " + instance.z);
We can clearly see that the derived field initializer is called first, prior to the base constructor.
Looking at the disassembled IL we can see that the field initializers code is placed at the beginning of the constructor code, prior to the base constructor call:
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
// Code size 48 (0x30)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldsfld int32 ConsoleApplication1.Base::index
IL_0006: dup
IL_0007: ldc.i4.1
IL_0008: add
IL_0009: stsfld int32 ConsoleApplication1.Base::index
IL_000e: stfld int32 ConsoleApplication1.Derived::y
IL_0013: ldarg.0
IL_0014: call instance void ConsoleApplication1.Base::.ctor()
IL_0019: nop
IL_001a: nop
IL_001b: ldarg.0
IL_001c: ldsfld int32 ConsoleApplication1.Base::index
IL_0021: dup
IL_0022: ldc.i4.1
IL_0023: add
IL_0024: stsfld int32 ConsoleApplication1.Base::index
IL_0029: stfld int32 ConsoleApplication1.Derived::z
IL_002e: nop
IL_002f: ret
} // end of method Derived::.ctor
We can see, obviously, that the compiler is protecting us from doing silly mistakes like accessing base fields before they’re initialized:
public class Base
{
protected int baseField = 100;
}
public class Derived : Base
{
public int x = baseField;
}
Leads to a compilation error, while this code works perfectly:
public class Derived : Base
{
public int x;
public Derived()
{
x = baseField;
}
}
I guess that in almost all cases this will not cause noticeable difference. It will however make a difference when we’re depending on that order, or, when the field initializers call methods that depend on side effects. So for start it’s important that we understand how our code works.
The next thing we can do is to use the field initializers as a hack to precede base constructors code. For example, if we have this 3rd party logger:
public class MyLogger
{
public MyLogger()
{
if (!Directory.Exists(@"C:\Logs"))
{
throw new InvalidOperationException("Can't start logs");
}
File.WriteAllText(@"C:\Logs\Session.log", "Initialized");
}
}
And we want to change it’s behavior without rerouting all it’s methods (of course this is not the only way), we can initialize a dummy field with method call that creates the directory:
public class MyLoggerHack : MyLogger
{
private bool dummy = CreateDirectory();
private static bool CreateDirectory()
{
if (!Directory.Exists(@"C:\Logs"))
{
Directory.CreateDirectory(@"C:\Logs");
}
return true;
}
}
So using this hack, we can “add” new code to the beginning of an existing call constructor without actually changing the original class.
The code complexity extension for Visual-Studio is now compatible with 2012. The new version has just been uploaded to the gallery.
The new version can be downloaded from Visual-Studio Gallery and the source code from CodePlex.
In this post I’ll present a usage of runtime method replacer in AOP context. The idea behind it is to change the behavior of an application without changing the IL of its methods. In this post I’ll show how to log an exception from a method.
This post is based on the work of Ziad Elmalki who posted the original method replacer. It is also based on the updated code for the method replacer by Chung Sung which is compatible with the new .NET framework versions. Lastly thanks to Roy Osherove who mentioned those recently.
The method replacer uses the following concept – after a method is jitted it receives a pointer of the jitted code. You can see how to extract that address in the original post. After extracting the addresses, we can simply replace one method with another:
public static void ReplaceMethod(IntPtr srcAdr, IntPtr destAdr)
{
unsafe
{
if (IntPtr.Size == 8)
{
ulong* d = (ulong*)destAdr.ToPointer();
*d = (ulong)srcAdr.ToInt64();
}
else
{
uint* d = (uint*)destAdr.ToPointer();
*d = (uint)srcAdr.ToInt32();
}
}
}
As a simple example, if we have these two methods:
public class MyClass
{
public static void Foo()
{
Console.WriteLine("In Foo");
throw new Exception("I am done here!");
}
public static void Bar()
{
Console.WriteLine("In Bar");
}
}
Then executing Foo in the following context:
MethodInfo barMethod = typeof (MyClass).GetMethod("Bar");
MethodInfo fooMethod = typeof (MyClass).GetMethod("Foo");
MethodUtil.ReplaceMethod(barMethod, fooMethod);
MyClass.Foo();
Will actually lead to the next result:
Which is… Cool!
What I’d like to present is a simplified example of how to catch an exception in business code without modifying it. A similar functionality to PostSharp exception handling. What we’re about to do is to hijack the original calls to Foo and redirect those to our new wrapper method. Our new wrapper method will call the original one inside a try/catch block.
Since we’re about to intercept calls to Foo based on its address, we’d like to store a “way” to call the original method later. The “way” to do it is simple, we’ll extract the method address before starting the interception and create a delegate to it using marshaling. The delegate will be stored on a field:
MethodInfo fooMethod = typeof (MyClass).GetMethod("Foo");
IntPtr fooAdress = MethodUtil.GetMethodAddress(fooMethod);
OriginalFoo = Marshal.GetDelegateForFunctionPointer(fooAdress, typeof (Action));
For the purpose of this example we could prepare a stub in the project istelf. But, in order to prove that it is likely possible to create a more general solution, we will generate the wrapper at runtime.
Since the wrapper is going to receive the calls instead of Foo it must have the same signature. Besides, our wrapper will retrieve the original Foo delegate from a static field named OriginalFoo. The delegate will be called from the method inside a try/catch block.
We will generate a dynamic method that replaces the original method:
// The field holding the delegate to the original Foo
FieldInfo originalFooDelegateField = typeof (FooProtector).GetField("OriginalFoo");
MethodInfo invokeDelegateMethod = OriginalFoo.GetType().GetMethod("DynamicInvoke");
MethodInfo innerExceptionGetter = typeof(Exception).GetProperty("InnerException").GetGetMethod();
MethodInfo exceptionMessageGetter = typeof(Exception).GetProperty("Message").GetGetMethod();
var dynamicMethod = new DynamicMethod("FooProtector", typeof(void), new Type[0]);
ILGenerator ilGenerator = dynamicMethod.GetILGenerator();
Label beginExceptionBlock = ilGenerator.BeginExceptionBlock();
// Preparing the call to the original Foo -
// Load the original Foo
ilGenerator.Emit(OpCodes.Ldsfld, originalFooDelegateField);
// Load "no arguments" to invoke the delegate
ilGenerator.Emit(OpCodes.Ldnull);
// Invoke the delegate and call original Foo
ilGenerator.Emit(OpCodes.Callvirt, invokeDelegateMethod);
ilGenerator.Emit(OpCodes.Pop);
ilGenerator.Emit(OpCodes.Leave, beginExceptionBlock);
ilGenerator.BeginCatchBlock(typeof (Exception));
// Extract the exception message
ilGenerator.Emit(OpCodes.Callvirt, innerExceptionGetter);
ilGenerator.Emit(OpCodes.Callvirt, exceptionMessageGetter);
// Print the exception message
MethodInfo info = typeof (Console).GetMethod("WriteLine", new[] {typeof (string)});
ilGenerator.Emit(OpCodes.Call, info);
ilGenerator.Emit(OpCodes.Leave, beginExceptionBlock);
ilGenerator.EndExceptionBlock();
ilGenerator.Emit(OpCodes.Ret);
// Trigger method compilation
dynamicMethod.CreateDelegate(typeof (Action));
This wrapper calls the original method through a delegate. In case an exception is thrown, it extracts the original exception and prints to the console the message.
Let’s revisit the original code and update it to the protecting code:
FooProtector.ProtectFoo();
MyClass.Foo();
The expected result is two messages printed, where the second one is the exception message “I am done here!”. As we can happily see, this is the exact result:
The concept of replacing methods using their jitted versions can be useful. It can be used to for AOP where it can be used for logging, exception handling and basically applying any custom aspect. It can also be used to modify some 3rd party code behavior for which we have no source code. Additionally, as Roy says is can be used as an engine for mocking frameworks.
But there are some disadvantages too. Firstly, it is very dependent on the compilation outcome which makes it quite fragile. Secondly, it is sensitive to optimizations, for example inlined methods cannot be handled. Thirdly, when it is used extensively it requires generation and JIT of many dynamic methods which might lead to a performance hit.
The switch statement controls the execution flow using comparison of constant values. This implies that in each case, the possible values are of primitive or Enum types. What can we do if we want to use a custom class as the switch expression? Clearly this is impossible by default, since custom classes are not primitives or Enums. The solution is to use the implicit cast.
Implicit cast can is implemented using the implicit cast operator. Implicit cast enables, for example, assignment of instance from one type into a variable of other type.
Example of implicitly castable class:
public class Person
{
public string ID { get; set; }
static public implicit operator int (Person person)
{
return int.Parse(person.ID.Substring(0, 1));
}
}
This casting allows this simple assignment:
var person = new Person() {ID = "0-12345678-9"};
int quality = person;
In order to use a class instance as the switch expression we must choose a governing type for the cases (primitive or Enum) and make the original class castable to that type. For example, if we choose a class of type Person and governing type int then we must have an implicit cast from Person to int.
For example, using the class Person:
var person = new Person() {ID = "0-12345678-9"};
switch (person)
{
case 0:
Console.WriteLine("This is a top level person");
break;
case 9:
Console.WriteLine("This is a bottom level person (probably a developer)");
break;
default:
Console.WriteLine("Just a regular person");
break;
}
In this post I’ll introduce a less known feature of the C# language – the goto expression.
In general, we know that it is not a great idea to use goto statements in code since it raises significantly the chance of ending up with spaghetti code. Reading and understanding of spaghetti code is way more difficult and therefore its maintenance is expensive.
The case we are going to deal with is goto inside single switch statement. It results that all the labels are local – this case is usually much easier to track than distanced labels. The goto statements enable us to implement the fall through capability which is not supported out of the box in C#.
In C# implicit fall through between cases in C# is not allowed. The reason for disallowing it is simple – implicit fall through is likely to cause bugs since forgetting to place a break is very common mistake. C# switch statements require each case to end with an explicit end point or a sure “never ending” code. End points are explicit expressions that transfers the execution out of the case block, these can be break, goto, throw and return. A never ending code is code that we know for sure during compilation it is never going to exit the block, such as a while loop whose condition is true.
According to these requirements it is clear that implicit fall through is not supported since we provide explicit instruction about how each case ends.
goto in C# is simple and consistent with most languages like C and C++. It is based on labels, which are defined in the code by ‘label-name:’ followed by a C# statement. In order to jump to a label we use the goto statement with the label – ‘goto label-name;’’.
This is a simple implementation of a loop with based on goto.
public void GotoBasedLoop()
{
int times = 3;
int i = 0;
start:
if (i >= times)
{
goto end;
}
Console.WriteLine(i);
i++;
goto start;
end:
return;
}
As we’ve seen – each case must have an end point or a promise it never ends. It leads that using naïve usage of gotos we can implement the fall through – we place a label at the beginning of every case and use a goto at the end of every case we want to redirect.
Luckily, for this case C# provides a labels like behavior that simplifies the usage. Each case can be redirected to other case using a simple syntax – goto case ‘constant’ or goto default. Using this syntax we can avoid a duplicate label for fall through.
This usage can be exampled by a simple code that “counts” asterisks:
static int GetLength(string s)
{
int x = 0;
switch (s)
{
case "*":
x++;
break;
case "**":
x++;
goto case "*";
case "***":
x++;
goto case "**";
}
return x;
}
As can be seen, each case is redirected to a different case using the built in mechanism.
Fall through is probably not the most common feature that developers look for. Since implicit fall through is more likely to cause bugs than simplify coding the C# designers decided to leave it out of the language. For those cases where we want to keep things simple – avoid extracting code to a method just for reuse or instead to create code duplication – we can use the simple solution of redirecting cases using goto.
In this post I’ll try to explain why it’s better not to accept changes or new tasks during an iteration. The trigger for this post is an experience I had a few weeks ago. I worked on a very interesting project, it’s a project with short iterations and a daily demo. The client is amazing – smart, responsive and very creative. The client acknowledges that not everything is known in advance and many changes are expected along the way. So far, so good. However, there is a downside – the involvement can make the feedback loop too short – as feedback gets translated into tasks within the same iteration.
The goal of agile development process is to deliver products in constant throughput, respond to changes and get to the final release with well working software that provides the exact value the client needed. In order to do all that both parties must stay realistic and take the right risks.
One of the nice ideas in agile is the iteration plan. During the iteration plan the client gets to tell what’s the most important features, the developers get to ask for details and finally estimate how much effort the feature requires. Since iteration plan includes all the developers planned to work on the features, all get the chance to contribute their point of view about each feature. The points of view are what’s the best way to implement (perhaps base the code on existing solution), they can talk about risks (this feature influences the performance, requires redesign, etc.), when everybody is well involved they point on more missing details in the specifications. All these allow us the reduce the risk – we get better effort estimation and we know better if we should expect side effects.
When a ticket is added during the iteration, it’s planning gets less focus – it is either gets planned by less team members or it gets planned by developers who do it while being distracted by the task they’re already in. The result is usually the we get less accurate estimations.
We all know that in order to get better software we need better code. With better code we have fewer bugs and easier maintenance. A very well known way to achieve this is having the developers code in “the zone” for as long as possible. Being in the zone requires understanding the problem, finding a solution and being able to incrementally implement it. All agile flavors have a good way in getting the developers in the zone. The idea is same – separating concerns. There’s a defined period of time devoted to non-development tasks, like iteration plan, demos, etc. The rest of the time is focused on development, which is known to us as iteration, sprint and so on. During this time the developers get to work on a predefined tasks, tasks which they had a chance to think about for spec (with the client) and technically (during effort estimations). If the developers are required to step into a mini-phase of planning in the middle of the iteration, the thinking mode must go back to planning mode. This is very likely going to cause a loss of focus which leads to less efficient development.
This is the simplest part and the riskiest. It is clear that most software being built is limited by real world constraints – money and time. Since we can’t get all the features from the backlog done on the first iteration we prioritize. This means that out of a set of relevant features, we pick the most important ones. Using this simple rationale we get to maximize the value of the developed software. What happens when a feature sneaks into the iteration? Usually it doesn’t really go through the usual prioritizing mechanism. Mostly commonly the reason for adding a feature is either it looks like it can “seal a feature” or it can solve some local problem. Both reasons provide value, but none promise to maximize the value.
Events are a classic implementation of the observer pattern. Support for events syntax exists in many languages, such as C#. In this post I’ll explain the internals of events.
The most abstract way to describe a delegate is a “pointer to a method”. A very relevant feature of delegates is that they can “point” at multiple methods. In order to do so we use the =+ operator and combine to other delegates, for example:
[Test]
public void Invoke_TwoDelegatesCombined_BothCalled()
{
bool wasACalled = false;
bool wasBCalled = false;
Action delA = () => wasACalled = true;
Action delB = () => wasBCalled = true;
Action combine = null;
combine += delA;
combine += delB;
combine.Invoke();
Assert.That(wasACalled);
Assert.That(wasBCalled);
}
But, what actually happens here? Let’s take a look at this code:
combine += delA;
This code compiles to the following IL:
IL_0031: ldloc.2
IL_0032: ldloc.0
IL_0033: call class [mscorlib]System.Delegate [mscorlib]System.Delegate::Combine(class [mscorlib]System.Delegate, class [mscorlib]System.Delegate)
IL_0038: castclass [mscorlib]System.Action
IL_003d: stloc.2
Which is equivalent to:
combine = Delegate.Combine(combine, delA);
The result of the compiled code is direct call to Delegate.Combine, which makes any future call to the combined result be forwarded to both delegates.
If we use default event implementation, the compiler generates two methods and a backing field. The backing field is a delegate storing the subscribers; the methods are add and remove the subscribers from the delegate. This implementation allows us to add and remove subscribers for whom the event is visible and raise the event from the type itself. For example:
public class Publisher
{
public event EventHandler MyEvent;
public void Publish()
{
MyEvent(this, EventArgs.Empty);
}
}
The event compiles into a field, which is a delegate of the event type:
.field private class [mscorlib]System.EventHandler MyEvent
And into two methods for adding and removing subscribers:
.event [mscorlib]System.EventHandler MyEvent
{
.addon instance void Events.Publisher::add_MyEvent(class [mscorlib]System.EventHandler)
.removeon instance void Events.Publisher::remove_MyEvent(class [mscorlib]System.EventHandler)
}
With the signatures:
.method public hidebysig specialname
instance void add_MyEvent (
class [mscorlib]System.EventHandler 'value'
) cil managed
.method public hidebysig specialname
instance void remove_MyEvent (
class [mscorlib]System.EventHandler 'value'
) cil managed
The bodies of the events, not surprisingly, manipulate the backing field; we can ignore the bodies for now.
So up to here we see what the declaration of event compiles into – an event declaration, a backing field which is a delegate of the event type and two methods for adding and removing subscribers. All this magic from a single C# line of code.
The other side of the event is what happens as we raise it. The event can be raised only from within the type that declares it. For example:
MyEvent(this, EventArgs.Empty);
Compiles into:
IL_0000: nop
IL_0001: ldarg.0
IL_0002: ldfld class [mscorlib]System.EventHandler Events.Publisher::MyEvent
IL_0007: ldarg.0
IL_0008: ldsfld class [mscorlib]System.EventArgs [mscorlib]System.EventArgs::Empty
IL_000d: callvirt instance void [mscorlib]System.EventHandler::Invoke(object, class [mscorlib]System.EventArgs)
All this code does is accessing the delegate backing field and invoking it.
In fact, the event is not custom but the add/remove methods are. Custom add/remove for events is a feature in C# which I think is not very commonly used (in contrast to properties). It allows the developer to provide an alternative implementation to the event subscription.
public event EventHandler MyCustomEvent
{
add {}
remove { }
}
The compiled class in this case does not contain a backing field. It contains the declaration of the event and the two methods with the custom provided body.
A difference which derives from the compiled code difference is that there’s no way to raise the event directly. This makes sense since the custom code can do many things (or nothing) with the subscribers and not store them in a common place for later invocation. If we try to raise MyCystomEvent in the same way we tried to raise MyEvent we’ll get a compilation error.
In the previous post we compared some alternatives of the dynamic keyword. One important and very interesting alternative is based on reflection emit. Reflection emit enables us to generate code using IL at runtime, compile it and execute it straightaway.
In this post we’ll see how to extract a string property named ‘Name’ from an unknown type using a dynamic method.
public static string GetNameByDynamicMethod(object arg)
{
Type type = arg.GetType();
Func<object, string> getterDelegate;
if (!typeToEmitDelegateMap.TryGetValue(type, out getterDelegate))
{
string typeName = type.Name;
PropertyInfo nameProperty = type.GetProperty("Name");
Type returnType = typeof (string);
// Define a new dynamic method
// The method returns a string type
// The method expects single parameter
var method = new DynamicMethod("GetNameFrom" + typeName,
returnType, new[] {typeof(object)});
ILGenerator ilGenerator = method.GetILGenerator();
// Load to the stack the first method argument.
//In our case, this is an object whose type we already know
ilGenerator.Emit(OpCodes.Ldarg_0);
// Cast the object to the type we already know
ilGenerator.Emit(OpCodes.Castclass, type);
// Call the getter method on the casted instance
ilGenerator.EmitCall(OpCodes.Call, nameProperty.GetGetMethod(), null);
// Return the value from Name property
ilGenerator.Emit(OpCodes.Ret);
// Compile the method and create a delegate to the new method
getterDelegate = (Func<object, string>)method.CreateDelegate(typeof(Func<object, string>));
typeToEmitDelegateMap.Add(type, getterDelegate);
}
return getterDelegate(arg);
}
What we did here was to define a new method, generate its code with IL, compile it and execute it. This new method is equivalent in many ways to a method we had generated in the original program. This new method will be hosted in a dynamic module in the memory.
The advantage of this kind of method over reflection is that it compiles the code once and doesn’t need to explore the type again whenever we need to get the property value.
A quick comparison for calling these alternatives 10,000,000 times each:
Seconds | Ratio to directly | |
Directly | 0.0131 | 1 |
Dynamic | 0.4609 | 35 |
Expression | 0.9154 | 70 |
Reflection emit | 0.9832 | 75 |
As can be seen, using the dynamic keyword works much faster than compiling an expression or a dynamic method at runtime.
Another interesting data set shows the time that each alternative takes to set up (The time to perform the first call):
Seconds | |
Directly | 0.00003 |
Dynamic | 0.08047 |
Expression | 0.00114 |
Reflection emit | 0.02169 |
In .NET 4.0 a new keyword was introduced: the dynamic keyword. One of the things it allows is calling methods on an instance and bypassing the compile time type checks. It can be useful in many scenarios, for example duck typing.
In this post, we’ll see that in some cases the keyword might have an unnecessary performance hit. Another thing we’ll see is how to save some of that time.
Let’s compare the performance of 3 ways of getting a property value – directly, using dynamic and using reflection:
public static string GetName(Student arg)
{
return arg.Name;
}
public static string GetNameByDynamic(dynamic arg)
{
return arg.Name;
}
public static string GetNameByReflection(object arg)
{
Type type = arg.GetType();
MethodInfo getter;
if (!typeToMethodMap.TryGetValue(type, out getter))
{
PropertyInfo property = type.GetProperty("Name");
getter = property.GetGetMethod();
typeToMethodMap.Add(type, getter);
}
return (string) getter.Invoke(arg, null);
}
Calling each method 10,000,000 times sums to: GetName=0.02 seconds, GetNameByDynamic=0.47 seconds, GetNameByReflection=15.41. Meaning, dynamic compared to strong type call is ~20 times slower.
One way to deal with this performance hit is to extract an interface from all possible objects, through using it we can get back to work with strong type:
public interface INameProvider{
string Name { get; set; }
}
And change our method to:
public static string GetNameByInterface(INameProvider arg)
{
return arg.Name;
}
Luckily this code runs at 0.07 seconds, which is ~7 times faster than the dynamic version. The conclusion from this is that if our code is in a critical performance area, we better extract an interface (as long as it makes sense – don’t abuse the interface if the types have no logical commonality!).
What should we do if our code is written in pre-.NET 4.0 version and our solution is based on reflection? In this case, our code runs ~750 times slower than the strong type version. Since we can’t use dynamic, which was introduced first at .NET 4.0, we should find some other solution. A simple one is generating a method using expressions. The main advantage of expressions here is that they can be compiled into a new method which we can reuse.
public static string GetNameByExpression(object arg)
{
Type type = arg.GetType();
Func<object, string> getterDelegate;
if (!typeToDelegateMap.TryGetValue(type, out getterDelegate))
{
var parameterExpression = Expression.Parameter(typeof (object), "arg");
var castExpression = Expression.TypeAs(parameterExpression, type);
MemberExpression memberExpression = Expression.Property(castExpression, "Name");
var lambda = Expression.Lambda<Func<object, string>>(memberExpression, parameterExpression);
getterDelegate = lambda.Compile();
typeToDelegateMap.Add(type, getterDelegate);
}
return getterDelegate(arg);
}
This code here is basically equivalent to generating a lambda which looks like:
(object arg) => ((Student)arg).Name;
After we compile the code once we can skip the reflection invocation each time and end with much faster code. Running this method times 10,000,000 takes 0.86 seconds, which is ~18 times faster than the reflection solution.
If you are writing code which must run as fast as possible, this is the performance summary:
Seconds | Ratio to directly | |
Directly | 0.02 | 1 |
Through interface | 0.07 | 3.5 |
Using dynamic | 0.47 | 23.5 |
Using expression | 0.86 | 43 |
Reflection | 15.41 | 770 |