需要哈希表和数组列表

发布于 2024-10-17 11:39:35 字数 394 浏览 2 评论 0原文

我正在尝试在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

笑,眼淚并存 2024-10-24 11:39:35

非泛型集合(包括 ArrayListHashTable)不包含在 Silverlight 中。
这些类是 .Net 1.0(没有泛型)的遗留物,不应在新代码中使用。

相反,您应该使用通用集合 - ListDictionary

The non-generic collections, including ArrayList and HashTable, 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> and Dictionary<TKey, TValue>.

两相知 2024-10-24 11:39:35

您有几个选择:

  1. 将导入更改为using System.Collections.Generic;,并将HashTable的每次使用更改为Dictionary<>< /code> 和 ArrayListList


  2. 可能能够逃脱:

    使用 HashTable = System.Collections.Generic.Dictionary;
    使用 ArrayList = System.Collections.Generic.List;
    请注意,任何未来的维护者都会讨厌你这样做。

  3. 但最好重构代码以正确使用通用集合。

  4. 在命名空间 System.Collections 中创建一个类 Hashtable,通过将所有内容转发到内部 来实现 IDictionary >Dictionary 并实现必要的行为更改(锁定、丢失密钥等);通过封装List创建一个ArrayList。 (henon建议)

You have a few options:

  1. Change your import to using System.Collections.Generic; and change every use of a HashTable to Dictionary<> and ArrayList to List<>.

  2. 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.

  3. But it's better to refactor the code to use the generic collections properly.

  4. Create a class Hashtable in a namespace System.Collections, implement IDictionary<object, object> by forwarding everything to an inner Dictionary<object, object> and implement the necessary changes in behavior (locking, missing keys, etc.); Create an ArrayList by encapsulation List<object>. (suggested by henon)

携余温的黄昏 2024-10-24 11:39:35

根据您使用的 .NET 框架,有不同的 mscorlib。如果您查看 MSDN 页面上的“其他版本”下拉列表,您将看到 Hashtable 不是 Silverlight 的一部分。您将需要使用 Dictionary (或者理想情况下更强类型的键和值类型)

Hashtable 不在 Silverlight

但是字典是

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 a Dictionary<Object, Object> (or ideally more strongly typed keys and value types)

Hashtable is not in Silverlight

But Dictionary is

转角预定愛 2024-10-24 11:39:35

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.

居里长安 2024-10-24 11:39:35

它对我来说很有效:

Hashtable for Dictionary

NameValueCollection for Dictionary

我遇到的其他问题是 Encoding.ASCII 也没有定义,我用一个 stackoverflow 小伙子写的函数对此进行了排序:

public static byte[] StringToAscii(string s) {
    byte[] retval = new byte[s.Length];
    for (int ix = 0; ix < s.Length; ++ix) {
        char ch = s[ix];
        if (ch <= 0x7f) retval[ix] = (byte)ch;
        else retval[ix] = (byte)'?';
    }
    return retval;
}

此处鸣谢:

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:

public static byte[] StringToAscii(string s) {
    byte[] retval = new byte[s.Length];
    for (int ix = 0; ix < s.Length; ++ix) {
        char ch = s[ix];
        if (ch <= 0x7f) retval[ix] = (byte)ch;
        else retval[ix] = (byte)'?';
    }
    return retval;
}

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()));

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文