views:

87

answers:

2

A classes Type metadata can be obtained in several ways. Two of them are:

var typeInfo = Type.GetType("MyClass")

and

var typeInfo = typeof(MyClass)

The advantage of the second way is that typos will be caught by the compiler, and the IDE can understand what I'm talking about (allowing features like refactoring to work without silently breaking the code)

Does there exist an equivalent way of strongly referencing members/properties/methods for metadata and reflection? Can I replace:

var propertyInfo = typeof(MyClass).GetProperty("MyProperty")

with something like:

var propertyInfo = property(MyClass.MyProperty)

+11  A: 

No, unfortunately not. It's been discussed and even named: infoof (pronounced "in-foof" for comedy value) but it's not been implemented... yet. Eric Lippert has a blog post about it.

The closest you can come in C# 3 is to make the compiler generate an expression tree, and then pull it out of that - but that's hardly pleasant.

Jon Skeet
+1  A: 

No, there is no such syntax in c#.

Andrew Bezzub