是否有一个C#等价的typeof的属性/方法/成员? [英] Is there a C# equivalent of typeof for properties/methods/members?
问题描述
一个班键入
元数据可以通过多种方式获得。其中两个是:
A classes Type
metadata can be obtained in several ways. Two of them are:
VAR所属类别= Type.GetType(MyClass的)
和
VAR所属类别= typeof运算(MyClass的)
的第二种方法的优点是错别字会被编译器捕获,并且IDE能明白我在说什么(允许像重构特性,而不默默地突破code工作)
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的)
的东西,如:
VAR的PropertyInfo =属性(MyClass.MyProperty)
推荐答案
没有,可惜没有。它已经讨论过,甚至命名为: infoof
(内foof发音为喜剧值),但它没有得到执行......呢。埃里克利珀有一个关于它的博客文章一>
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.
您可以来在C#3是使编译器生成的前pression树,然后拉出来的是最近的 - 但是这是很难令人愉快
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.
这篇关于是否有一个C#等价的typeof的属性/方法/成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!