tags:

views:

155

answers:

1

Howdy.

Does anyone know how to write an ExpressionHelper.GetName method so I can get the name of a public static/const property or field using Reflection and C# 3.0

So for

static class B
{
  public const string Field = "mittens";
  public static string Prop = "the kitten";
}

The following asserts would work (the syntax is obviously not going to work)

Assert.AreEqual(ExpressionHelper.GetName(B.Field),"Field");
Assert.AreEqual(ExpressionHelper.GetName(B.Prop),"Prop");

The important thing is for compile time errors to appear when someone changes these fields/props.

Thanks!

+1  A: 

The article LINQ beyond queries: strong-typed reflection could offer a solution. I read it several months ago - so I cannot offer a definit solution at the moment. But may be it helps.

Daniel Brückner