get_PropertyName()/set_PropertyName() 与 PropertyName?
我正在使用对正在使用的公共 API 的程序集的反射以及 System.CodeDOM
来生成一些通过 API 提取信息的代码。
在我的自动生成代码的一部分中,我引用了 API 程序集中的许多类型属性的值。但是,我最终总是引用生成的代码中实际不存在的属性。我使用了 Type.GetProperties() ,据我所知,它应该只返回公共属性。
我进一步研究发现,当我缺少一个属性(例如名为 SampleProperty
)时,类中有两个方法,分别为 get_SampleProperty
和 set_SampleProperty
> 但没有实际的 SampleProperty
属性。
这是怎么回事?为什么智能感知将这些方法视为单独的方法,但是当通过反射返回时,它们显示为属性?
I'm using reflection on the assembly of a public API I am working with along with System.CodeDOM
to generate some code that will extract information through the API.
In part of my auto-generated code I am referencing the values of a number of properties of types in the API assembly. However, I keep ending up with references to properties that don't actualy exist in my generated code. I used Type.GetProperties()
which from what I understand should only return public properties.
I looked into it further and found that when I had a missing property, say called SampleProperty
there were instead two methods in the class called get_SampleProperty
and set_SampleProperty
but no actual SampleProperty
property.
What's going on here? Why does intellisense treat these methods as separate methods, but when when returned through reflection they show up as a property?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能是您的第一个难题,PropertyInfo 类没有 GetProperties 方法。 Type 类可以。否则,您的问题表明您实际上正在使用 Type.GetMethods()。是的,这会返回属性的 get_Blah 和 set_Blah 属性访问器方法。在底层,属性实际上是作为方法实现的。
使用 Type.GetProperties() 来反映属性。
That might be your first hang-up, the PropertyInfo class doesn't have a GetProperties method. The Type class does. Your question otherwise indicates that you are actually using Type.GetMethods(). Yes, that returns the get_Blah and set_Blah property accessor methods for a property. Under the hood, properties are actually implemented as methods.
Use Type.GetProperties() to reflect properties.