NHibernate的2.0:与IDictionary的Cfg.Configuration.SetProperties问题
-
20-08-2019 - |
题
我有一个问题:
NHibernate.Cfg.Configuration.SetProperties()
不接受的IDictionary:NHibernateConfigHandler
我得到的信息:
错误30的最好重载方法匹配 'NHibernate.Cfg.Configuration.SetProperties(System.Collections.Generic.IDictionary)' 具有一些无效参数
和
错误31参数 '1':不能从 'System.Collections.IDictionary' 转换为 'System.Collections.Generic.IDictionary'
请指教?
在整个方法:
/// <param name="config">NHibernate configuration</param>
public ISessionFactory GetSessionFactoryFor(NHibernateConfigHandler config)
{
if (config == null)
throw new ArgumentNullException("config may not be null nor empty");
ISessionFactory sessionFactory = GetSessionFactoryFor(config.MappingAssembly);
// Failed to find a cached SessionFactory so make a new one.
if (sessionFactory == null)
{
NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();
cfg.SetProperties(config.Properties); //THIS LINE
cfg.AddAssembly(config.MappingAssembly);
// Now that we have our Configuration object, create a new SessionFactory
sessionFactory = cfg.BuildSessionFactory();
if (sessionFactory == null)
{
throw new InvalidOperationException("cfg.BuildSessionFactory() returned null.");
}
HttpRuntime.Cache.Add(config.MappingAssembly, sessionFactory, null, DateTime.Now.AddDays(7),
TimeSpan.Zero, CacheItemPriority.High, null);
}
return sessionFactory;
}
解决方案
setProperties方法的签名是
public NHibernate.Cfg.Configuration SetProperties(System.Collections.Generic.IDictionary<string,string> newProperties)
Member of NHibernate.Cfg.Configuration
setProperties方法正在类型的IDictionary,即通用字典的参数。你想在IDictionary中通过。这就是为错误的原因。
要么可以在NHibernateConfigHandler改变属性的类型System.Collections.Generic.IDictionary。 要么 创建System.Collections.Generic.IDictionary和System.Collections.IDictionary复制所有值。
它总是有效的是使用通用的词典(这是对System.Collections.Generic)超过IDictionary的(这是System.Collections中上)。
不隶属于 StackOverflow