静态参数化类中关于其实例会发生什么?

发布于 2024-10-21 10:35:54 字数 425 浏览 4 评论 0原文

假设我有这个类:

public class DispatcherService<T>
{
    private static Action<T> Dispatcher;

    public static void SetDispatcher(Action<T> action)
    {
        Dispatcher = action;
    }

    public static void Dispatch(T obj)
    {
        Dispatcher.Invoke(obj);
    }
}

让我说清楚......对于每种类型,我只有一个 DispatcherService 实例,并且仅在我调用它时。正确的?

只是询问内存问题。

Suppose I have this class:

public class DispatcherService<T>
{
    private static Action<T> Dispatcher;

    public static void SetDispatcher(Action<T> action)
    {
        Dispatcher = action;
    }

    public static void Dispatch(T obj)
    {
        Dispatcher.Invoke(obj);
    }
}

Let me get this straight... I'll have only one instance of DispatcherService<T> for each type, and only when I call it. Right?

Just asking for memory issues.

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

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

发布评论

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

评论(2

谜兔 2024-10-28 10:35:54

我只有一个实例
每种类型的 DispatcherService

是。

并且仅当我调用它时。对吗?

是的。

该代码由 CLR 在需要使用时发出。


请注意

,如果我在你那里,我会将其更改为单例

public class DispatcherService<T>
{

    private static readonly DispatcherService<T> _dispatcher = new DispatcherService<T>();
    private Action<T> _action;

    private DispatcherService() {}

    public static DispatcherService<T> Dispatcher
    {
        get {return _dispatcher ;}
    }

    public void SetDispatcher(Action<T> action)
    {
        Dispatcher = action;
    }

    public void Dispatch(T obj)
    {
        Dispatcher.Invoke(obj);
    }
}

I'll have only one instance of
DispatcherService for each type

Yes.

and only when I call it. Right?

Yes.

The code is emitted by CLR when it needs to use it.


Note

if I where you I would change it to singleton.

public class DispatcherService<T>
{

    private static readonly DispatcherService<T> _dispatcher = new DispatcherService<T>();
    private Action<T> _action;

    private DispatcherService() {}

    public static DispatcherService<T> Dispatcher
    {
        get {return _dispatcher ;}
    }

    public void SetDispatcher(Action<T> action)
    {
        Dispatcher = action;
    }

    public void Dispatch(T obj)
    {
        Dispatcher.Invoke(obj);
    }
}
不交电费瞎发啥光 2024-10-28 10:35:54

您可以拥有任意数量的 DispatcherService 实例,因为该类可以自由实例化。另一件事是没有意义,因为它没有实例方法。如果不打算实例化它,您可以将其更改为 public static class DispatcherService,如此例所示。

对于每种类型,您最多还有一个 DispatcherService.Dispatcher 实例,这正是您可能想知道的。如果您不访问特定 TDispatcherService,则不会为该 T 分配任何资源。

You can have as many instances of DispatcherService as you like, since the class can be instantiated freely. It's another matter that there's no point to that because it has no instance methods. You can change it to public static class DispatcherService<T> if it's not meant to be instantiated, as in this example.

You will also have at most one instance of DispatcherService.Dispatcher for each type, which is what you probably want to know. If you don't access DispatcherService for a specific T, no resources will be allocated for that T.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文