需要哈希表和数组列表
我正在尝试在我的 Windows 7 Phone 应用程序中使用其他人的 C# 类。这些类使用 Hashtable 类型的对象。
有问题的文件
using System.Collections;
位于顶部,所以我假设这就是它想要的 Hashtable 对象。
当我尝试构建解决方案时,出现错误,提示无法找到类型或命名空间名称“Hashtable”,您是否缺少 using 指令或程序集引用。
在微软的Hashtable文档中,我看到它说Assembly: mscorlib
但是如果我尝试通过Project>Add Reference添加mscorlib,VS说它无法添加它,因为它是由构建系统自动引用的。
我缺少什么?
I am trying to use someone else's C# classes in my Windows 7 Phone app. The classes use objects of type Hashtable.
The file in question has
using System.Collections;
at the top, so I'm assuming that's the Hashtable object it wants.
When I try to build my solution, I get errors that the type or namespace name 'Hashtable' could not be found, are you missing a using directive or assembly reference.
In Microsoft's documentation of Hashtable, I see it says Assembly: mscorlib
But if I try to add mscorlib via Project>Add Reference, VS says it can't add it because it is automatically referenced by the build system.
What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
非泛型集合(包括
ArrayList
和HashTable
)不包含在 Silverlight 中。这些类是 .Net 1.0(没有泛型)的遗留物,不应在新代码中使用。
相反,您应该使用通用集合 -
List
和Dictionary
。The non-generic collections, including
ArrayList
andHashTable
, are not included in Silverlight.These classes are hold-overs from .Net 1.0 (which didn't have generics) and should not be used in new code.
Instead, you should use the generic collections—
List<T>
andDictionary<TKey, TValue>
.您有几个选择:
将导入更改为
using System.Collections.Generic;
,并将HashTable
的每次使用更改为Dictionary<>< /code> 和
ArrayList
到List
。您可能能够逃脱:
使用 HashTable = System.Collections.Generic.Dictionary
使用 ArrayList = System.Collections.Generic.List
但最好重构代码以正确使用通用集合。
在命名空间
System.Collections
中创建一个类Hashtable
,通过将所有内容转发到内部来实现
并实现必要的行为更改(锁定、丢失密钥等);通过封装IDictionary
>DictionaryList
You have a few options:
Change your import to
using System.Collections.Generic;
and change every use of aHashTable
toDictionary<>
andArrayList
toList<>
.You might be able to get away with:
using HashTable = System.Collections.Generic.Dictionary<object, object>;
using ArrayList = System.Collections.Generic.List<object>;
Note that any future maintainer will hate you for doing this.
But it's better to refactor the code to use the generic collections properly.
Create a class
Hashtable
in a namespaceSystem.Collections
, implementIDictionary<object, object>
by forwarding everything to an innerDictionary<object, object>
and implement the necessary changes in behavior (locking, missing keys, etc.); Create anArrayList
by encapsulationList<object>
. (suggested by henon)根据您使用的 .NET 框架,有不同的 mscorlib。如果您查看 MSDN 页面上的“其他版本”下拉列表,您将看到
Hashtable
不是 Silverlight 的一部分。您将需要使用Dictionary
(或者理想情况下更强类型的键和值类型)There are different mscorlibs depending on which .NET framework you are using. If you look in the "Other Versions" dropdown on the MSDN page, you will see
Hashtable
is not a part of Silverlight. You will need to use aDictionary<Object, Object>
(or ideally more strongly typed keys and value types)System.Collection 是 .Net 第一个版本的遗产 - 没有泛型类型。
要修复您的代码,请使用 Dictorany 类,它本质上是一个哈希表,
和 List 插入 ArrayList。
System.Collection is a legacy of first version of .Net - no generic types.
To fix your code use Dictorany class which is a hashtable at heart,
and List insted of ArrayList.
它对我来说很有效:
Hashtable for
Dictionary
NameValueCollection for
Dictionary
我遇到的其他问题是 Encoding.ASCII 也没有定义,我用一个 stackoverflow 小伙子写的函数对此进行了排序:
此处鸣谢:
Windows Phone 7 中的 ASCIIEncoding
所以最后要返回 ASCII,需要执行以下操作:
return StringToAscii(Encoding.Unicode.GetString(result.ToArray()));
It worked for me changing:
Hashtable for
Dictionary<object, object>
NameValueCollection for
Dictionary<object, object>
Other problem I encoutered is Encoding.ASCII is not defined either, I sorted that with a function a stackoverflow lad wrote:
credits here:
ASCIIEncoding In Windows Phone 7
So finally to return the ASCII this is what to do:
return StringToAscii(Encoding.Unicode.GetString(result.ToArray()));