IExtension:可扩展对象模式:它是如何工作的?
我正在探索使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Attach
和Detach
方法由ExtensionCollection.InsertItem
和ExtensionsCollection.RemoveItem
调用,分别。 这些方法又是由SynchronizedCollection.Add
和SynchronizedCollection.Remove
调用的虚拟方法,这是您的代码最终调用的方法。 这可以通过使用 Reflector 进行验证。 如果您创建了自己的IExtensionCollection
实现,则无法保证Attach
和Detach
会被正确调用。The
Attach
andDetach
methods are called byExtensionCollection<T>.InsertItem
andExtensionsCollection<T>.RemoveItem
, respectively. These methods, in turn, are virtual methods called bySynchronizedCollection<T>.Add
andSynchronizedCollection<T>.Remove
, which is what your code ends up calling into. This can be verified by using Reflector. If you created your own implementation ofIExtensionCollection<T>
, there's no guarantee thatAttach
andDetach
would be called correctly.