Monday, October 11, 2010

The vassal of my vassal is not my vassal

Just a thumb rule - avoid creating a PropertyExpression using

public static MemberExpression Expression.Property(Expression expression, MethodInfo property)

Instead prefer

public static MemberExpression Expression.Property(Expression expression, PropertyInfo property)

Tuesday, October 5, 2010

Interview

I would like to ask the following questions/tasks on a technical interview.
  1. Implement a singleton.
    Yes, kinda boring question. But I'll expect simple implementation, like follows:

    public class Singleton{
      private Singleton(){}
      public static Singleton Instance = new Singleton();
    }

    and nothing more. There is no words about laziness. Anything more complicated signals about over-engineering.
  2. Implement (I)Disposable pattern. And after it - When does a type need a finalizer?
    Second part is simple, first part is not good without explanation. In this case I'll wait for

    protected virtual void Dispose(bool isDisposing)

     
  3. ...to be continued

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