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

Sunday, August 22, 2010

#1 Params

A simple question. What would be printed out after execution?

void Main()
{
 Foo();
 Foo(null)
 Foo(null as object)
}

void Foo(params object[] args)
{
 if (args == null)
  Console.WriteLine("Null");
 else
  Console.WriteLine(args.Length);
}


* This source code was highlighted with Source Code Highlighter.

Thursday, August 19, 2010

#3 Enum.ToString()

It's kinda surprise, but Enum.ToString() uses reflection to get a string representation of an enum:
public override string ToString()
{
  Type type = base.GetType();
  object obj2 = ((RtFieldInfo) GetValueField(type)).InternalGetValue(this, false);
  return InternalFormat(type, obj2);
}


* This source code was highlighted with Source Code Highlighter.
This approach leads to visible performance issues when an application widely uses enum.ToString() (Obviously, any enum).

This issue could be fixed with the following trick code:
static class EnumHelper
{
  static class Cache<T>
  {
    internal static readonly Dictionary<T, string> Values = new Dictionary<T, string>();
  }
    
  public static string FastToString<T>(this T @enum) where T:struct
  { 
    if (!typeof(T).IsEnum)
      throw new ArgumentException(string.Format("Type {0} is not an enumeration.", typeof(T)));
    lock (Cache<T>.Values)
    {
      string result;
      if (!Cache<T>.Values.TryGetValue(@enum, out result))
      {
        result = @enum.ToString();
        Cache<T>.Values.Add(@enum, result);
      }
      return result;
    }
  }
}

* This source code was highlighted with Source Code Highlighter.
And, of course, all enum.ToString() should be replaced with enum.FastToString().