简体   繁体   English

通过元数据令牌访问PropertyInfo以便在IL中使用?

[英]Access PropertyInfo via metadata token for use from IL?

I have an application where I have a method taking a PropertyInfo parameter and would like to call this method from IL. 我有一个应用程序,我有一个方法采取PropertyInfo参数,并希望从IL调用此方法。 For similar methods taking a MethodInfo, for example, I can create an intermediate method taking a RuntimeMethodHandle and use GetMethodFromHandle . 例如,对于采用MethodInfo的类似方法,我可以创建一个采用RuntimeMethodHandle并使用GetMethodFromHandle的中间方法。 The IL can then use Ldtoken to pass the handle. 然后,IL可以使用Ldtoken传递句柄。

However, there does not appear to be an equivalent metadata token for properties. 但是,似乎没有属性的等效元数据标记。 I can see why this might be the case (since properties are really just a way of bundling methods together and never 'called' from IL), but there is definitely property metadata associated with the type. 我可以看到为什么会出现这种情况(因为属性实际上只是将方法捆绑在一起而不是从IL“调用”的方式),但肯定存在与该类型相关联的属性元数据。 I have access to this property metadata at Emit-time, so I'd like a way to be able to pass this directly without having to resort to Reflection by name at runtime (ie emit Reflection calls to GetProperty taking a string that will execute at runtime.) Is there a way to do this? 我可以在Emit-time访问这个属性元数据,所以我想要一种能够直接传递它的方法,而不必在运行时求助于名称的反射(即发出反射调用GetProperty获取将在运行时。)有办法做到这一点吗?


Per a request in the comments, here is the application: 根据评论中的请求,这是应用程序:

I'm creating an adaptor class that exposes a property reference as its component bits via a bool this[int index] property. 我正在创建一个适配器类,它通过bool this[int index]属性将属性引用公开为其组件位。 My application compiles PLC code to a .NET assembly and so I'm trying to create diagnostic accessors that approximate the easy bitwise access provided by the PLC (where you write MyTag.2 to indicate bit 2 of tag MyTag .) This syntax can't be used for consumption by C#, but PLC.GetBits().MyTag[2] is a reasonable approximation. 我的应用程序将PLC代码编译为.NET程序集,因此我正在尝试创建诊断访问器,这些访问器近似于PLC提供的简单按位访问(您在其中编写MyTag.2以指示标记MyTag 2位。)此语法可以'用于C#消费,但PLC.GetBits().MyTag[2]是一个合理的近似值。

My original approach was implemented using PropertyInfo (which is how I encountered this issue), but I can certainly work around it by passing the applicable metadata from the PropertyInfo as multiple parameters. 我的原始方法是使用PropertyInfo实现的(这是我遇到此问题的方法),但我当然可以通过将PropertyInfo中的适用元数据作为多个参数传递来解决它。 I was mainly just curious to see if it was possible to pass the PropertyInfo directly, since I hadn't ever run into this before. 我主要只是想知道是否可以直接传递PropertyInfo,因为我之前从未遇到过这种情况。

No, I don't think you can. 不,我认为你不能。 I say this in part through familiarity with that API, and in part because the Expression compiler in the C# compiler still uses reflection when it refers to a PropertyInfo , but uses more direct methods ( ldtoken etc) when referring to types and methods (for example, a getter/setter). 我通过熟悉该API来部分说明这一点,部分原因是C#编译器中的Expression编译器在引用PropertyInfo时仍然使用反射,但在引用类型和方法时使用更直接的方法( ldtoken等)(例如,一个吸气剂/二传手。 I suspect the C# compiler team would have used it if it existed. 我怀疑C#编译器团队会在它存在的情况下使用它。

However, in most common IL-emit scenarios, it is not necessary to pass around a PropertyInfo . 但是,在大多数常见的IL-emit场景中, 没有必要传递PropertyInfo options: 选项:

  • use MethodBase to get the getter or setter (methods can be fetched by token), and infer the property by name (not 100% robust, but should usually work) 使用MethodBase获取getter或setter(方法可以通过令牌获取),并按名称推断属性(不是100%健壮,但通常应该工作)
  • pass around the name instead (ldstr) 传递名称而不是(ldstr)

Refer to Ecma-335 , Partition I, 8.10.3 Property and event inheritance 请参阅Ecma-335 ,分区I,8.10.3 属性和事件继承

Fundamentally, properties and events are constructs of the metadata intended for use by tools that target the CLI and are not directly supported by the VES itself . 从根本上说,属性和事件是旨在供CLI使用的工具使用的元数据的构造,并且VES本身不直接支持这些元数据。 Therefore, it is the job of the source language compiler and the reflection library (see Partition IV – Kernel Package) to determine rules for name hiding, inheritance, and so forth. 因此,源语言编译器和反射库(参见Partition IV - Kernel Package)的工作是确定名称隐藏,继承等规则。 The source compiler shall generate CIL that directly accesses the methods named by the events and properties, not the events or properties themselves. 源代码编译器应生成直接访问事件和属性命名的方法的CIL,而不是事件或属性本身。

Ecma-335, Partition I, 8.11.3 Property definitions Ecma-335,Partition I,8.11.3 属性定义

A property definition is always part of either an interface definition or a class definition. 属性定义始终是接口定义或类定义的一部分。 The name and value of a property definition is scoped to the type that includes the property definition. 属性定义的名称和值的范围限定为包含属性定义的类型。 The CTS requires that the method contracts that comprise the property shall match the method implementations, as with any other method contract. CTS要求包含属性的方法契约应与方法实现匹配,与任何其他方法契约一样。 There are no CIL instructions associated with properties , just metadata. 没有与属性关联的CIL指令 ,只有元数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM