2015-08-04 7 views
6

मेरे पास एक स्थिर वर्ग है जिसमें मैं शब्दकोशों का उपयोग .NET प्रकारों और एसक्यूएल प्रकारों के बीच मैप करने के लिए लुकअप टेबल के रूप में कर रहा हूं। यहाँ इस तरह के एक शब्दकोश का एक उदाहरण है:एक ConcurrentDictionary कैसे शुरू करें? त्रुटि: "निजी विधि का उपयोग नहीं कर सकते 'यहां जोड़ें'

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 प्रकार में गुजरता है और यह इसी एमएस एसक्यूएल सर्वर इस शब्दकोश का उपयोग कर प्रकार की स्ट्रिंग मान देता है। हालांकि, चूंकि इसका डेटाबेस डेटाबेस बनाने के लिए लुकअप टेबल के रूप में उपयोग किया जा रहा है, मुझे लगता है कि इसे एक 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 की यानी सभी प्रमुख मान जोड़े) के भीतर लाल को रेखांकित करता है और त्रुटि "है यहाँ निजी विधि तक नहीं पहुंच पा 'जोड़ें' मुझे नहीं पता ऐसा लगता है क्योंकि मैंने इसे निजी स्थैतिक रूप से पढ़ा है, क्योंकि मैंने अभी एक सार्वजनिक स्थिर संस्करण बनाकर परीक्षण किया है और मुझे एक ही त्रुटि मिलती है।

उत्तर

10

संग्रह प्रारंभकर्ता जो आप संग्रह को पॉप्युलेट करने के लिए उपयोग कर रहे हैं केवल संग्रह करता है यदि संग्रह में है एक Add उपयुक्त हस्ताक्षर और अभिगम्यता की विधि। ConcurrentDictionary में सार्वजनिक Add विधि नहीं है, इसलिए आप संग्रह प्रारंभिक उपयोग करने में सक्षम नहीं होंगे इसके साथ एर

आप निर्माता के लिए एक पैरामीटर के रूप में एक IEnumerable<KeyValuePair<TKey, TValue>> पास करके कुछ प्रारंभिक डेटा प्रदान कर सकते हैं, या आप ConcurrentDictionary बनाने के बाद TryAdd (या AddOrUpdate, या किसी अन्य नाम पर Add साथ तरीकों में से) एक पाश में कॉल कर सकते हैं।

+0

आह ठीक है, मैं देखता हूं, धन्यवाद। काश मैं इसे पहले से ही 'स्थिर ConcurrentDictionary' में शुरू कर सकता था क्योंकि मुझे पता है कि यह बदलने वाला नहीं है और मैं बस इसे जल्दी से एक्सेस करना चाहता हूं। मैं देखूंगा कि मैं क्या कर सकता हूं। – Drew

+0

@ ड्रू और आप संग्रह प्रारंभकर्ता के उपयोग के बजाय, कन्स्ट्रक्टर को 'आईन्यूमेरेबल' पास करके कर सकते हैं। – Servy

+0

ओह, मैंने आपके उत्तर में जो मतलब बताया, उसे गलत समझा, धन्यवाद! – Drew

0

जैसा कि @ उत्तर ने अपने उत्तर में कहा, संग्रह प्रारंभ Add विधि के साथ प्रकारों के लिए काम करता है। लेकिन यह भी काम करना चाहिए, अगर Add नाम के साथ एक्सटेंशन विधि और उपयुक्त हस्ताक्षर मौजूद है। तो आप समवर्ती शब्दकोश के लिए एक बना सकते हैं। प्रारंभिक रूप से थ्रेड-सुरक्षित होगा, वास्तव में आप स्थैतिक क्षेत्र प्रारंभकर्ता का उपयोग कर रहे हैं।

5

एक 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"} 
}); 
0

Servy के स्वीकार किए जाते हैं जवाब देने के लिए एक कोड उदाहरण के रूप में की कोशिश करो, तुम एक प्रकार है कि निर्माता को KeyValuePair प्रकार के (एक List की तरह) IEnumerable impements पारित कर सकते हैं :

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") 
     }); 
संबंधित मुद्दे