Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why not a memberinfo() reflection function for C# [duplicate]

There is sizeof() and typeof(), but why not a memberinfo() returning an instance of System.Reflection.MemberInfo for the part of code selected in order to aid in reflection code.

Example:

Program() 
{
       Type t = typeof(Foo);

       Foo foo = new Foo();
       PropertyInfo pi = memberinfo(Foo.Name) as PropertyInfo;
       // or shall it be like this
       // PropertyInfo pi = memberinfo(foo.Name) as PropertyInfo;

       string name = pi.GetValue(foo, null);
}

I am trying to understand if there is a fundamental reason why this could be implemented in the C# spec.

I am not bashing anything, I am just doing some wishful thinking, so be kind please.

like image 788
John Alexiou Avatar asked Nov 19 '10 21:11

John Alexiou


1 Answers

Eric Lippert talks about this extensively on his blog

To quote directly from that post:

Just off the top of my head, here are a few {reasons why this hasn't been done}. (1) How do you unambiguously specify that you want a method info of an specific explicit interface implementation? (2) What if overload resolution would have skipped a particular method because it is not accessible? It is legal to get method infos of methods that are not accessible; metadata is always public even if it describes private details. Should we make it impossible to get private metadata, making the feature weak, or should we make it possible, and make infoof use a subtly different overload resolution algorithm than the rest of C#? (3) How do you specify that you want the info of, say, an indexer setter, or a property getter, or an event handler adder?

like image 83
Wesley Wiser Avatar answered Nov 10 '22 03:11

Wesley Wiser