get_PropertyName()/set_PropertyName() 与 PropertyName?

发布于 2024-10-05 22:58:52 字数 436 浏览 0 评论 0原文

我正在使用对正在使用的公共 API 的程序集的反射以及 System.CodeDOM 来生成一些通过 API 提取信息的代码。

在我的自动生成代码的一部分中,我引用了 API 程序集中的许多类型属性的值。但是,我最终总是引用生成的代码中实际不存在的属性。我使用了 Type.GetProperties() ,据我所知,它应该只返回公共属性。

我进一步研究发现,当我缺少一个属性(例如名为 SampleProperty)时,类中有两个方法,分别为 get_SamplePropertyset_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野稚 2024-10-12 22:58:52

我使用了 PropertyInfo.GetProperties(),据我了解,它应该只返回公共属性。

这可能是您的第一个难题,PropertyInfo 类没有 GetProperties 方法。 Type 类可以。否则,您的问题表明您实际上正在使用 Type.GetMethods()。是的,这会返回属性的 get_Blah 和 set_Blah 属性访问器方法。在底层,属性实际上是作为方法实现的。

使用 Type.GetProperties() 来反映属性。

I used PropertyInfo.GetProperties() which from what I understand should only return public properties.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文