如何使用F#类型提供程序在构造函数定义中实现基础构造函数调用

7
我将尝试在C#中创建一个提供的类型,以以下方式调用其基础构造函数:
public class SubclassController : BaseClass
{
    public SubclassController (IntPtr handle) : base (handle)
    {}
}

我目前能够提供的最接近的翻译是这样的:

我目前能够提供的最接近的翻译是这样的:

public sealed class SubclassController : BaseClass
{
    public SubclassController (IntPtr handle)
    {
        this;
        base..ctor (handle);
    }

尽管具有相同的功能,但两者并不完全相同。 我用来构建ProvidedConstructor的代码如下:
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 -> <@@ () @@>
1个回答

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 -> <@@ () @@>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接