Access PropertyInfo through a metadata token to use from IL?

I have an application in which I have a method that takes a PropertyInfo parameter and would like to call this method from IL. For example, for similar methods using MethodInfo, I can create an intermediate method using RuntimeMethodHandle and using GetMethodFromHandle . IL can then use the Ldtoken to transmit the descriptor.

However, no equivalent marker metadata exists for properties. I can understand why this may be so (since properties are just a way to combine methods together and never be "called" from IL), but there are certain property metadata associated with the type. I have access to this property metadata in Emit-time, so I would like to be able to pass this directly without resorting to Reflection by name at runtime (i.e. emit Reflection calls GetProperty, taking the line that will be executed in runtime .) Is there a way to do this?


In the request in the comments, here is the application:

I am creating an adapter class that provides a property reference as its component bits through the bool this[int index] property. My application compiles PLC code to a .NET assembly, and so I am 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 the MyTag tag.) This syntax cannot be used for consumption on C #, but PLC.GetBits().MyTag[2] is a reasonable approximation.

My original approach was implemented using PropertyInfo (that's how I came across this problem), but I can, of course, get around it by passing the applicable metadata from PropertyInfo as a few parameters. I was just curious to know if it is possible to pass PropertyInfo directly, since I have not come across this before.

+4
source share
2 answers

No, I don’t think you can. I'm talking about this partly because I am familiar with this API, and partly because the Expression compiler in the C # compiler still uses reflection when it refers to PropertyInfo , but uses more direct methods ( ldtoken , etc.), When it refers to types and methods (e.g. getter / setter). I suspect that the C # compiler command would use it if it existed.

However, in most common IL-emit scripts, there is no need to go around PropertyInfo . Options:

  • use MethodBase to get getter or setter (methods can be extracted using a token) and display a property by name (not 100% reliable, but should usually work)
  • skip the name instead (ldstr)
+4
source

Refer to Ecma-335 , Section I, 8.10.3 Inheriting Properties and Events

In essence, properties and events are metadata constructs intended for use by tools that are targeted at the CLI and are not directly supported by VES itself. Thus, it is the task of the source language compiler and reflection library (see section IV - kernel package) for defining rules for hiding names, inheritance, etc. The source compiler should generate a CIL that directly accesses methods called events and properties, not the events or the properties themselves.

Ecma-335, Section I, 8.11.3 Property Definitions

A property definition is always part of an interface definition or class definition. The name and value of the property definition refers to the type that the property definition includes. CTS requires that the method of contracts that include the property must conform to the implementation of the method, as in any other contract. There are no CIL instructions related to properties, only metadata.

+3
source

Source: https://habr.com/ru/post/1446776/


All Articles