jeudi 17 février 2022

Failure to discover property with reflection

Taking the following minimal example:

type IMyInterface =
    interface
        abstract member Name: string with get
    end

let testInstance =
    { new IMyInterface with
        member _.Name = "Hello Word" }

I would have naively expected a call to testInstance.GetType().GetProperties() to contain a PropertyInfo element corresponding to Name.

However, only an empty array is returned.

Using testInstance.GetType().GetProperty("Name") yields no better as it simply returns a <null> object.

More confusing still, Visual Studio 2022 IntelliSense lists Name as a valid property (as I'd expect).

How can I get a PropertyInfo corresponding to the Name property?





Aucun commentaire:

Enregistrer un commentaire