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.
PropertyInfo
? I've done tons of IL work, and I can't remember needing aPropertyInfo
in the generated code. If one of the accessors would do, you can useMethodBase.GetMethodFromHandle
on either the getter or setter method, which returns aMethodBase
object that can be castclass to aMethodInfo
. What is the context where-by you need aPropertyInfo
? – Pshaw