通过类名字符串获取类实例

发布于 2024-09-03 20:45:31 字数 1947 浏览 5 评论 0原文

我注意到 D 中的函数 Object.factory(char[] className) 。 但它并没有像我希望的那样工作;它不起作用;)

一个例子:

import std.stdio;

class TestClass
{
    override string toString()
    {
        return typeof(this).stringof; // TestClass
    }
};

void main(string[] args)
{
    auto i = Object.factory("TestClass");
    if (i is null)
    {
        writeln("Class not found");
    }
    else
    {
        writeln("Class string: " ~ i);
    }
}

我认为这应该会导致消息:“类字符串:TestClass”,但它说“找不到类”。

有谁知道为什么会发生这种情况以及我该如何解决它?

或者我需要创建自己的类工厂吗?例如,通过使用静态数组Object[string]classes;和类实例创建一个类。当我想要一个新实例时,我这样做:

auto i = (className in classes);
if (i is null)
{
    return null;
}
return i.classinfo.create();

编辑:

我现在像这样使用它(一个例子,这是一个网络 HMVC 模式):

class Page : Controller
{
    static this()
    {
        register(Page.classinfo);
    }

    protected void registerActions()
    {
        registerAction("index", &index);
    }

    public void index()
    {
        request.response = "Page: " ~ request.params.get("pageID", "0") ~ " in format: " ~ request.params.get("format", "html");
    }
};

void main(string[] args)
{
    Route.add(
        r"page/(\d+)\.(html|json)",
        [
            1: "pageID",
            2: "format"
        ],
        [
            "controller": "page" // tell route to use page as controller class
        ]
    );
    Route.add(
        r"(\S+)/(\S+)",
        [
            1: "controller", // get controller class from uri
            2: "action" // get controller action from uri
        ]
    );

    auto request = Request.factory("/page/43.json").execute();

    // Headers and response can be accessed like this
    // Can be used in http response
    uint code = request.getCode();
    const(string[string]) headers = request.getHeaders();
    string response = request.response;
}

这种东西在 C++ 中很难做到;)

I noticed the function Object.factory(char[] className) in D.
But it does not work like I hoped it would work; it does not work ;)

An example:

import std.stdio;

class TestClass
{
    override string toString()
    {
        return typeof(this).stringof; // TestClass
    }
};

void main(string[] args)
{
    auto i = Object.factory("TestClass");
    if (i is null)
    {
        writeln("Class not found");
    }
    else
    {
        writeln("Class string: " ~ i);
    }
}

I think this should result in the message: "Class string: TestClass" but it says "Class not found".

Does anybody know why this happens and how I could fix it ?

Or do I need to make my own class factory. For example by make a class with a static array Object[string] classes; with class instances. When I want a new instance I do this:

auto i = (className in classes);
if (i is null)
{
    return null;
}
return i.classinfo.create();

EDIT:

I use it now like this (an example, this is for a web HMVC pattern):

class Page : Controller
{
    static this()
    {
        register(Page.classinfo);
    }

    protected void registerActions()
    {
        registerAction("index", &index);
    }

    public void index()
    {
        request.response = "Page: " ~ request.params.get("pageID", "0") ~ " in format: " ~ request.params.get("format", "html");
    }
};

void main(string[] args)
{
    Route.add(
        r"page/(\d+)\.(html|json)",
        [
            1: "pageID",
            2: "format"
        ],
        [
            "controller": "page" // tell route to use page as controller class
        ]
    );
    Route.add(
        r"(\S+)/(\S+)",
        [
            1: "controller", // get controller class from uri
            2: "action" // get controller action from uri
        ]
    );

    auto request = Request.factory("/page/43.json").execute();

    // Headers and response can be accessed like this
    // Can be used in http response
    uint code = request.getCode();
    const(string[string]) headers = request.getHeaders();
    string response = request.response;
}

This kind of stuff is hard to do in C++ ;)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

画▽骨i 2024-09-10 20:45:31

这是一个可行的方法:

module irc2;

import std.stdio;

class TestClass
{
    override string toString()
    {
        return typeof(this).stringof; // TestClass
    }
};

void main(string[] args)
{
    auto i = Object.factory("irc2.TestClass");
    if (i is null)
    {
        writeln("Class not found");
    }
    else
    {
        writeln("Class string: " ~ i.toString);
    }
}

需要注意的一些事项:

  1. 您必须使用完全限定的类名。如果您的程序中有多个“TestClass”怎么办?
  2. 您不能将对象附加到字符串;你必须使用toString。或者简单地使用 writefln("Class string: %s", i) 。

Here's one that works:

module irc2;

import std.stdio;

class TestClass
{
    override string toString()
    {
        return typeof(this).stringof; // TestClass
    }
};

void main(string[] args)
{
    auto i = Object.factory("irc2.TestClass");
    if (i is null)
    {
        writeln("Class not found");
    }
    else
    {
        writeln("Class string: " ~ i.toString);
    }
}

A few things to note:

  1. You have to use the fully-qualified class name. What if you had more than one "TestClass" in your program.
  2. You can't append an object to a string; you have to use toString. That, or simply use writefln("Class string: %s", i).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文