それは十分に単純ですが、マスター辞書に値を追加することはできません。これは、辞書に単純な型ではなくオブジェクト要素があり、「masterDict.Add」メソッドの構文が正しくないためだと思います。一部のオブジェクト値が設定されていること、一部が空の文字列、一部が null であることはわかっていますが、これが問題の原因ではないと思います。オブジェクトは存在します。「newMsg.Value」でクラッシュします。
私のエラー:
System.NullReferenceException was unhandled
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Example Code:
public class msg
{
public string msgId { get; set; }
public string msgType { get; set; }
public string lastName { get; set; }
public string firstName { get; set; }
public string dateOfBirth { get; set; }
public string sex { get; set; }
public string pAddress { get; set; }
public string pPhone { get; set; }
public IEnumerable<string> prodCode { get; set; }
public string dateOfServiceText { get; set; }
}
public static Dictionary<String, msg> masterDict { get; set; }
public static Dictionary<String, msg> tmpDict = new Dictionary<String, msg>()
{
{ "1111", new msg { msgId = "1111", msgType = "DFT", firstName="Sachin" }},
{ "1112", new msg { msgId = "1112", msgType = "DFT", firstName="Dina" }},
{ "1113", new msg { msgId = "1113", msgType = "DFT", firstName="Andy" }}
};
public static void mergeDict()
{
//now insert your new values into the master
foreach (var newMsg in tmpDict)
if (newMsg.Value != null)
masterDict.Add(newMsg.Key, newMsg.Value);
}
static void Main(string[] args)
{
mergeDict();
}