如何获取class的构造方法?

How to get the constructor method of a class?

我有一个 class A 需要另一个 class B 作为模板参数,我需要在中获取 B 的构造函数为了做(有点伪代码):

class A(B)
{
    import std.typecons : Tuple;
    import std.traits : Parameters;
    Tuple!(Parameters!B) _args;
    this(Parameters!B args)
    {
        _args = args;
    }
}

为构造函数存储参数,然后使用存储在 _args.

中的参数构造 class B 的对象

这几乎就是命令模式。

有没有办法让B的构造函数得到它的Parameters? 或者有没有更好的方法来实现延迟对象构造?

是的,构造函数的内部名称是__ctor:

class C
{
    this(int a, string b) { }
}

import std.traits;

pragma(msg, Parameters!(C.__ctor));

这输出:

(int, string)