Access PropertyInfo via metadata token for use from IL?
Asked Answered
C

2

5

I have an application where I have a method taking a PropertyInfo parameter and would like to call this method from IL. For similar methods taking a MethodInfo, for example, I can create an intermediate method taking a RuntimeMethodHandle and use GetMethodFromHandle. The IL can then use Ldtoken to pass the handle.

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. 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 (i.e. emit Reflection calls to GetProperty taking a string that will execute at runtime.) Is there a way to do this?


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. 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.

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. 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.

Covarrubias answered 18/11, 2012 at 15:54 Comment(4)
You can call GetAccessors on the PropertyInfo to get a pair of MethodInfo objects for its getter and setter. You can also call GetGetMethod for the getter and GetSetMethod for the setter.Flatiron
@dasblinkenlight, I'm considering this option, but it basically requires replacing all of the methods currently taking PropertyInfo with parameters for the various parts of the metadata I need. There are various boundary cases here (such as indexer properties.)Covarrubias
Do you actually need the PropertyInfo? I've done tons of IL work, and I can't remember needing a PropertyInfo in the generated code. If one of the accessors would do, you can use MethodBase.GetMethodFromHandle on either the getter or setter method, which returns a MethodBase object that can be castclass to a MethodInfo. What is the context where-by you need a PropertyInfo?Pshaw
You can keep PropertyInfo all the way until the point when you must produce IL, at which point the IL producing method would grab the getter or the setter, based on the access the code must perform.Flatiron
P
4

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). I suspect the C# compiler team would have used it if it existed.

However, in most common IL-emit scenarios, it is not necessary to pass around a 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)
  • pass around the name instead (ldstr)
Pshaw answered 18/11, 2012 at 16:16 Comment(1)
Ah, interesting note on the Expression compiler; that's pretty strong evidence that it's not directly accessible.Covarrubias
F
3

Refer to Ecma-335, Partition I, 8.10.3 Property and event inheritance

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. 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. The source compiler shall generate CIL that directly accesses the methods named by the events and properties, not the events or properties themselves.

Ecma-335, Partition I, 8.11.3 Property definitions

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. There are no CIL instructions associated with properties, just metadata.

Franzoni answered 4/5, 2013 at 23:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.