为什么不的MemberInfo()用于反射C#功能 [英] Why not a memberinfo() reflection function for C#

查看:1221
本文介绍了为什么不的MemberInfo()用于反射C#功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的sizeof() typeof运算(),但为什么不是的MemberInfo ()的代码,以便在反射代码,以帮助选定的部分返回 System.Reflection.MemberInfo 的一个实例。

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.

例如:

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);
}



我想知道,如果有,为什么这可能实现的根本原因在C#规范。

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.

推荐答案

这个埃里克利珀广泛的会谈在他的博客

Eric Lippert talks about this extensively on his blog

要直接从该职位引述如下:

To quote directly from that post:

刚刚送走我的头的顶部,这里有几个{原因,这还没有做到}。 (1)你如何明确指定要一个具体明确的接口实现的方法的信息? (2)如果重载会跳过了特定的方法,因为它无法访问?它是合法获得的不可访问的方法的方法的相关信息;元数据始终是公开的,即使它描述的私人资料。我们应该让不可能得到私人的元数据,使功能弱,或者我们应该有可能,使infoof使用比C#的休息微妙的不同重载解析算法? (3)你如何指定你想要的,说的信息,一个索引二传手,或属性getter,或者一个事件处理加法?

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?

这篇关于为什么不的MemberInfo()用于反射C#功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆