使用F#类型提供程序如何实现基础构造函数调用作为构造函数定义的一部分

7sh*_*rp9 7 f# type-providers

我想要做的是创建一个提供的类型,在C#中调用它的基本构造函数:

public class SubclassController : BaseClass
{
    public SubclassController (IntPtr handle) : base (handle)
    {}
}
Run Code Online (Sandbox Code Playgroud)

我目前最接近的是:

public sealed class SubclassController : BaseClass
{
    public SubclassController (IntPtr handle)
    {
        this;
        base..ctor (handle);
    }
Run Code Online (Sandbox Code Playgroud)

虽然具有相同的功能但并不完全相同.

我用来构建ProvideConstructor的代码如下:

let providedConstructor = ProvidedConstructor([ProvidedParameter("handle", typeof<IntPtr>)])
let ctorInfo = typeof<SubclassController>.GetConstructor(BindingFlags.Public ||| BindingFlags.Instance, null, [|typeof<IntPtr>|], null)
providedConstructor.BaseConstructorCall <- fun args -> ctorInfo, args
providedConstructor.InvokeCode <- fun args -> <@@ () @@>
Run Code Online (Sandbox Code Playgroud)

7sh*_*rp9 3

这实际上是 ProvidedTypes.fs 中的一个错误,现已在最新版本中修复。可以从Codeplex的老地方购买感谢@desco,

因此,此代码实际上是正确形成的基本构造函数调用所需的全部内容:

let providedConstructor = ProvidedConstructor([ProvidedParameter("handle", typeof<IntPtr>)])
let ctorInfo = typeof<SubclassController>.GetConstructor(BindingFlags.Public |||         BindingFlags.Instance, null, [|typeof<IntPtr>|], null)
providedConstructor.BaseConstructorCall <- fun args -> ctorInfo, args
providedConstructor.InvokeCode <- fun args -> <@@ () @@>
Run Code Online (Sandbox Code Playgroud)