最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】C#中,Dictionary转换出错:Additional information: Unable to cast object of type ‘System.Collections.Generic.Dictionary`2[System.String,System.Object]’ to type ‘System.Collections.Generic.Dictionary`2[System.String,System.String]’

C# crifan 9972浏览 0评论

【问题】

这段C#代码:

//"L" : "http://g-ecx.images-amazon.com/images/G/01/kindle/dp/2012/KC/KC-slate-01-lg._V401028090_.jpg", 
//"S" : "http://g-ecx.images-amazon.com/images/G/01/kindle/dp/2012/KC/KC-slate-01-sm._V401028090_.jpg"

//"L" : "http://g-ecx.images-amazon.com/images/G/01/kindle/dp/2012/KC/KC-slate-03-lg._V400694812_.jpg",
//"S" : "http://g-ecx.images-amazon.com/images/G/01/kindle/dp/2012/KC/KC-slate-03-sm._V400694812_.jpg",
//"rich": {
//    src: "http://g-ecx.images-amazon.com/images/G/01/misc/untranslatable-image-id.jpg",
//    width: null,
//    height: null
//}

Type curType = imgUrlObj.GetType();
Dictionary<string, string> imgUrlDict = (Dictionary<string, string>)imgUrlObj;
string largeImgUrlStr = "";
if (imgUrlDict.TryGetValue("L", out largeImgUrlStr))
{
    imageUrlList[idx] = largeImgUrlStr;
    gotImageUrlOk = true;
}

涉及到,将Object的imgUrlObj,转换为所希望的:Dictionary<string, string>

其中包含的值为:

obj value contain dict strings

对应的其Type类型为:

curType = {Name = "Dictionary`2" FullName = "System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Object, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e0…

obj type is dictionary 2

结果转换就出错了:

An unhandled exception of type ‘System.InvalidCastException’ occurred in ScrapeAmazonProduct.exe

Additional information: Unable to cast object of type ‘System.Collections.Generic.Dictionary`2[System.String,System.Object]’ to type ‘System.Collections.Generic.Dictionary`2[System.String,System.String]’.

Additional information Unable to cast object of type System.Collections.Generic.Dictionary 2

【解决过程】

1.之前一直没看懂。

现在大概看懂了。

说是不能把:

System.Collections.Generic.Dictionary`2[System.String,System.Object]

转换为:

System.Collections.Generic.Dictionary`2[System.String,System.String]

所以,我们需要把对应的string换成Object:

Type curType = imgUrlObj.GetType();
Dictionary<string, Object> imgUrlDict = (Dictionary<string, Object>)imgUrlObj;
Object largeImgUrObj = "";
if (imgUrlDict.TryGetValue("L", out largeImgUrObj))
{
    imageUrlList[idx] = largeImgUrObj.ToString();
    gotImageUrlOk = true;
}

试试结果,就OK了:

can covert obj to dict string obj

 

【总结】

在将Object转换为原先以为的

Dictionary<string, string>

其实只是针对于这样的内容:

"L" : "http://g-ecx.images-amazon.com/images/G/01/kindle/dp/2012/KC/KC-slate-01-lg._V401028090_.jpg",

"S" : "http://g-ecx.images-amazon.com/images/G/01/kindle/dp/2012/KC/KC-slate-01-sm._V401028090_.jpg"

是可以的。

但是后面还会有别的情况的:

"L" : "http://g-ecx.images-amazon.com/images/G/01/kindle/dp/2012/KC/KC-slate-03-lg._V400694812_.jpg",

"S" : "http://g-ecx.images-amazon.com/images/G/01/kindle/dp/2012/KC/KC-slate-03-sm._V400694812_.jpg",

"rich": {

    src: "http://g-ecx.images-amazon.com/images/G/01/misc/untranslatable-image-id.jpg",

    width: null,

    height: null

}

此时,就必须用

Dictionary<string, Object>

了。否则就会报错:

Unable to cast object of type ‘System.Collections.Generic.Dictionary`2[System.String,System.Object]’ to type ‘System.Collections.Generic.Dictionary`2[System.String,System.String]’.

了。

 

心得是:

要十分注意你所转换的对象的真正的类型,和当前的值。

不要搞错类型了。

转载请注明:在路上 » 【已解决】C#中,Dictionary转换出错:Additional information: Unable to cast object of type ‘System.Collections.Generic.Dictionary`2[System.String,System.Object]’ to type ‘System.Collections.Generic.Dictionary`2[System.String,System.String]’

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.173 seconds, using 22.13MB memory