Tuesday, September 21, 2010

Equals vs Equals

Could you create an example, when System.Object.Equals(a,b) will return true and a.Equals(b)will return false


And moreover, why does your example works and what paradigm have you violated to achieve the result?

Wednesday, September 15, 2010

default(Type)

Just another stupid question. In C# there is a default keyword, that allows to get a default value for a given type - null for reference types and some defined value for value types. Actually, if T is value type, "default(T)" is equals to "new T()". This keyword works in compile time and a compiler will substitute it with corresponding value.
But how could I get a default value in runtime, when I have System.Type object?

PropertyInfo.SetValue()

Consider the following example.

We have a class named Foo:
class Foo
{
public int Bar{get;set;}
}


What is the result of the following code execution?

var property = typeof(Foo).GetProperty("Bar");
Foo foo = new Foo {Bar = 5};
property.SetValue(foo, null, null);
Console.WriteLine(foo.Bar);

Monday, September 13, 2010

Difference between reference type and value type

Consider the following example. You need to create a dynamic method with the following signature:
void SetValue(object instance, object value)
{
}

The method should set the given value to the specified property in the instance object. Property should be specified by its PropertyInfo.
Alas, Expressions in .NET 3.5 don't allow to create complex expressions and functors. So the solution is to use DynamicMethod from System.Reflection.Emit:

Thursday, September 2, 2010

How to start dump analysis with WinDbg

Just a side note.
  1. Open a dump of interest
  2. Load SOS extensions by executing
    .loadby sos mscorwks
  3. If you receive the error like "Failed to load data access DLL", check your symbol path with ".sympath" command. To set the correct sympath, use
    .sympath srv*c:\mycache*http://msdl.microsoft.com/download/symbols
    and then reload dlls with
    .cordll -ve -u -l