IExtension:可扩展对象模式:它是如何工作的?

发布于 2024-07-16 15:18:52 字数 1988 浏览 8 评论 0原文

我正在探索使用 C# 中的 (IExtension, IExtensibleObject) 接口的可扩展对象模式, 我提供了一个非常简单的示例,用于将本地日期转换为 UTC,反之亦然:

public class BaseObject : IExtensibleObject<BaseObject> 
{
    private DateTime _startDate;
    private ExtensionCollection<BaseObject> _extensions;

    public DateTime StartDate
    {
        get { return _startDate; }
        set { _startDate = value; }
    }

    public BaseObject()
    {
        StartDate = DateTime.Now;
        _extensions = new ExtensionCollection<BaseObject>(this); 
    }

    #region IExtensibleObject<BaseObject> Members

    public IExtensionCollection<BaseObject> Extensions
    {
        get 
        {
            return _extensions;
        }
    }

    #endregion
}

public class DateTimeConverterExtension : IExtension<BaseObject>
{
    private BaseObject _owner;

    #region IExtension<BaseObject> Members

    public void Attach(BaseObject owner)
    {
        _owner = owner;
        _owner.StartDate = owner.StartDate.ToUniversalTime();
    }

    public void Detach(BaseObject owner)
    {
        _owner.StartDate = _owner.StartDate.ToLocalTime();
    }

    #endregion
}

class Program
{
    static void Main(string[] args)
    {
        BaseObject obj = new BaseObject();
        Console.WriteLine("Local Time: "+obj.StartDate);

        obj.Extensions.Add(new DateTimeConverterExtension());


        Console.WriteLine("UTC: "+obj.StartDate);

        DateTimeConverterExtension ext = obj.Extensions.Find<DateTimeConverterExtension>();
        obj.Extensions.Remove(ext);

        Console.WriteLine("Local Time: "+obj.StartDate);
    }
}

输出:

当地时间:4/13/2009 11:09:19 AM
世界标准时间:2009 年 4 月 13 日上午 5:39:19
当地时间:2009年4月13日 11:09:19 AM
按任意键继续 。 。 .

所以它有效,问题是:

当程序调用 IExtensionCollection.Add 和 IExtensionCollection.Detach 方法时,.Net 框架如何调用“Attach”和“Detach”? 填写我在这里缺少的内部详细信息。

I was exploring about Extensible object pattern using (IExtension, IExtensibleObject) interfaces in C#,
I came with a very simple example to convert dates to UTC from local and vice-versa:

public class BaseObject : IExtensibleObject<BaseObject> 
{
    private DateTime _startDate;
    private ExtensionCollection<BaseObject> _extensions;

    public DateTime StartDate
    {
        get { return _startDate; }
        set { _startDate = value; }
    }

    public BaseObject()
    {
        StartDate = DateTime.Now;
        _extensions = new ExtensionCollection<BaseObject>(this); 
    }

    #region IExtensibleObject<BaseObject> Members

    public IExtensionCollection<BaseObject> Extensions
    {
        get 
        {
            return _extensions;
        }
    }

    #endregion
}

public class DateTimeConverterExtension : IExtension<BaseObject>
{
    private BaseObject _owner;

    #region IExtension<BaseObject> Members

    public void Attach(BaseObject owner)
    {
        _owner = owner;
        _owner.StartDate = owner.StartDate.ToUniversalTime();
    }

    public void Detach(BaseObject owner)
    {
        _owner.StartDate = _owner.StartDate.ToLocalTime();
    }

    #endregion
}

class Program
{
    static void Main(string[] args)
    {
        BaseObject obj = new BaseObject();
        Console.WriteLine("Local Time: "+obj.StartDate);

        obj.Extensions.Add(new DateTimeConverterExtension());


        Console.WriteLine("UTC: "+obj.StartDate);

        DateTimeConverterExtension ext = obj.Extensions.Find<DateTimeConverterExtension>();
        obj.Extensions.Remove(ext);

        Console.WriteLine("Local Time: "+obj.StartDate);
    }
}

Output:

Local Time: 4/13/2009 11:09:19 AM
UTC: 4/13/2009 5:39:19 AM
Local Time: 4/13/2009 11:09:19 AM
Press any key to continue . . .

So it works, the question is:

How does .Net framework invoke "Attach" and "Detach" when IExtensionCollection.Add and IExtensionCollection.Detach methods are invoked by the program? Fill in the internal details which I am missing here.

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

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

发布评论

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

评论(1

一个人的旅程 2024-07-23 15:18:52

AttachDetach 方法由 ExtensionCollection.InsertItemExtensionsCollection.RemoveItem 调用,分别。 这些方法又是由 SynchronizedCollection.AddSynchronizedCollection.Remove 调用的虚拟方法,这是您的代码最终调用的方法。 这可以通过使用 Reflector 进行验证。 如果您创建了自己的 IExtensionCollection 实现,则无法保证 AttachDetach 会被正确调用。

The Attach and Detach methods are called by ExtensionCollection<T>.InsertItem and ExtensionsCollection<T>.RemoveItem, respectively. These methods, in turn, are virtual methods called by SynchronizedCollection<T>.Add and SynchronizedCollection<T>.Remove, which is what your code ends up calling into. This can be verified by using Reflector. If you created your own implementation of IExtensionCollection<T>, there's no guarantee that Attach and Detach would be called correctly.

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