我有一个静态类,我在其中使用字典作为查找表来映射.NET类型和SQL类型。这是一个这样的字典的例子:
private static readonly Dictionary<Type, string> SqlServerMap = new Dictionary<Type, string>
{
{typeof (Boolean), "bit"},
{typeof (Byte[]), "varbinary(max)"},
{typeof (Double), "float"},
{typeof (Byte), "tinyint"},
{typeof (Int16), "smallint"},
{typeof (Int32), "int"},
{typeof (Int64), "bigint"},
{typeof (Decimal), "decimal"},
{typeof (Single), "real"},
{typeof (DateTime), "datetime2(7)"},
{typeof (TimeSpan), "time"},
{typeof (String), "nvarchar(MAX)"},
{typeof (Guid), "uniqueidentifier"}
};
然后我在下面有一个公共方法,它传入一个.NET类型,它使用这个字典返回相应的MS SQL Server类型的字符串值。但是,由于这被用作进行数据库查询的查找表,我认为将其设为ConcurrentDictionary
是有意义的。我改成了:
private static readonly IDictionary<Type, string> SqlServerMap = new ConcurrentDictionary<Type, string>
{
{typeof (Boolean), "bit"},
{typeof (Byte[]), "varbinary(max)"},
{typeof (Double), "float"},
{typeof (Byte), "tinyint"},
{typeof (Int16), "smallint"},
{typeof (Int32), "int"},
{typeof (Int64), "bigint"},
{typeof (Decimal), "decimal"},
{typeof (Single), "real"},
{typeof (DateTime), "datetime2(7)"},
{typeof (TimeSpan), "time"},
{typeof (String), "nvarchar(MAX)"},
{typeof (Guid), "uniqueidentifier"}
};
但现在它强调了{}
中的所有红色(即ConcurrentDictionary
的所有键值对),错误是:
无法在此处访问私有方法“添加”
我不认为这是因为我将它初始化为private static readonly
,因为我只是通过制作public static
版本进行测试,我得到了同样的错误。
只有当集合具有适当签名和可访问性的Add
方法时,您用于填充集合的集合初始值设定项才有效。 ConcurrentDictionary
没有公共Add
方法,因此您将无法使用它的集合初始化程序。
您可以通过将IEnumerable<KeyValuePair<TKey, TValue>>
作为参数传递给构造函数来提供一些初始数据,或者您可以在创建TryAdd
之后在循环中调用AddOrUpdate
(或Add
或名称中包含ConcurrentDictionary
的任何其他方法)。
试试这个
private static readonly IDictionary<Type, string> SqlServerMap =
new ConcurrentDictionary<Type, string>(
new Dictionary<Type, string>()
{
{typeof(Boolean ), "bit" },
{typeof(Byte[] ), "varbinary(max)" },
{typeof(Double ), "float" },
{typeof(Byte ), "tinyint" },
{typeof(Int16 ), "smallint" },
{typeof(Int32 ), "int" },
{typeof(Int64 ), "bigint" },
{typeof(Decimal ), "decimal" },
{typeof(Single ), "real" },
{typeof(DateTime), "datetime2(7)" },
{typeof(TimeSpan), "time" },
{typeof(String ), "nvarchar(MAX)" },
{typeof(Guid ), "uniqueidentifier"}
}
);
更新:如果您使用的是C#6(Roslyn 2.0编译器),则可以使用新的Dictionary Initializers。
private static readonly IDictionary<Type, string> SqlServerMap =
new ConcurrentDictionary<Type, string>
{
[typeof(Boolean )] = "bit" ,
[typeof(Byte[] )] = "varbinary(max)" ,
[typeof(Double )] = "float" ,
[typeof(Byte )] = "tinyint" ,
[typeof(Int16 )] = "smallint" ,
[typeof(Int32 )] = "int" ,
[typeof(Int64 )] = "bigint" ,
[typeof(Decimal )] = "decimal" ,
[typeof(Single )] = "real" ,
[typeof(DateTime)] = "datetime2(7)" ,
[typeof(TimeSpan)] = "time" ,
[typeof(String )] = "nvarchar(MAX)" ,
[typeof(Guid )] = "uniqueidentifier"
};
作为Servy接受的答案的代码示例,为了在实例化时初始化ConcurrentDictionary
,您可以传递一种类型,它将IEnumerable
(如List
)的KeyValuePair
类型强制转换为构造函数:
private static readonly IDictionary<Type, string> SqlServerMap =
new ConcurrentDictionary<Type, string>(
new List<KeyValuePair<Type, string>>
{
new KeyValuePair<Type, string>(typeof(Boolean), "bit"),
new KeyValuePair<Type, string>(typeof(Boolean), "bit"),
new KeyValuePair<Type, string>(typeof(Byte[]), "varbinary(max)"),
new KeyValuePair<Type, string>(typeof(Double), "float"),
new KeyValuePair<Type, string>(typeof(Byte), "tinyint"),
new KeyValuePair<Type, string>(typeof(Int16), "smallint"),
new KeyValuePair<Type, string>(typeof(Int32), "int"),
new KeyValuePair<Type, string>(typeof(Int64), "bigint"),
new KeyValuePair<Type, string>(typeof(Decimal), "decimal"),
new KeyValuePair<Type, string>(typeof(Single), "real"),
new KeyValuePair<Type, string>(typeof(DateTime), "datetime2(7)"),
new KeyValuePair<Type, string>(typeof(TimeSpan), "time"),
new KeyValuePair<Type, string>(typeof(String), "nvarchar(MAX)"),
new KeyValuePair<Type, string>(typeof(Guid), "uniqueidentifier")
});
由于您的收藏不会改变,您现在可以使用ImmutableDictionary。虽然这也有初始化的问题,但有针对此question about initialization提出的解决方案:
@LukášLánský提供的简单解决方案是
var d = new Dictionary<string, int> { { "a", 1 }, { "b", 2 } }.ToImmutableDictionary();
@IanGriffiths提供的表现更好的版本是
public struct MyDictionaryBuilder<TKey, TValue> : IEnumerable
{
private ImmutableDictionary<TKey, TValue>.Builder _builder;
public MyDictionaryBuilder(int dummy)
{
_builder = ImmutableDictionary.CreateBuilder<TKey, TValue>();
}
public void Add(TKey key, TValue value) => _builder.Add(key, value);
public TValue this[TKey key]
{
set { _builder[key] = value; }
}
public ImmutableDictionary<TKey, TValue> ToImmutable() => _builder.ToImmutable();
public IEnumerator GetEnumerator()
{
// Only implementing IEnumerable because collection initializer
// syntax is unavailable if you don't.
throw new NotImplementedException();
}
}
正如@Servy在他的回答中所说,集合初始化适用于使用Add
方法的类型。但是,如果存在名称为Add
和适当签名的扩展方法,它也应该有效。所以你可以为并发字典创建一个。由于您使用的是静态字段初始化程序,因此初始化将是线程安全的。