【发布时间】:2010-01-25 20:25:53
【问题描述】:
我有这个代码:
public class EntityMapper<T> where T : IMappingStrategy, new()
{
private static T currentStrategy;
public static T CurrentStrategy
{
get
{
if (currentStrategy == null)
currentStrategy = new T();
return currentStrategy;
}
}
}
然后:
public static void Main()
{
EntityMapper<ServerMappingStrategy>.CurrentStrategy.ToString();
EntityMapper<ClientMappingStrategy>.CurrentStrategy.ToString();
EntityMapper<ServerMappingStrategy>.CurrentStrategy.ToString();
}
好吧,问题是:
为什么我在调试的时候可以看到ServerBussinessMappingStrategy的构造函数只调用了一次?
这很好用,但我理解为什么 EntityMapper 总是返回我需要的正确实例,只实例化一次 ServerMappingStrategy 类。
问候!
PD:对不起,我的英语 jeje ;)
【问题讨论】:
标签: c# .net entity-framework static generics