如何在 Xamarin.Forms 便携式项目中使用 System.Collections
How to use System.Collections in a Xamarin.Forms Portable Project
所以,你好。
我对 .NET Framework 的 CookieContainer Class 有疑问。当我在 CookieContainer 中有 cookie 时,getCookies 无法获取 Cookies。 我认为问题出在 CookieContainer Class 的方法 getCookies() 上,因为我的域有很多子域,例如:
this.has.lots.of.subdomains.com
不知道,如果它是一个错误,或者如果我做错了什么,但我决定通过获取所有 Cookie 来解决这个问题,然后自己搜索
遍历我得到的所有 Cookie(大部分只有 2 - 3 个 Cookie,所以没什么大不了的。)
我在Whosebug上找到的方法是这个:
public static CookieCollection GetAllCookies(this CookieContainer cookieJar)
{
CookieCollection cookieCollection = new CookieCollection();
Hashtable table = (Hashtable)cookieJar.GetType().InvokeMember("m_domainTable",
BindingFlags.NonPublic |
BindingFlags.GetField |
BindingFlags.Instance,
null,
cookieJar,
new object[] { });
foreach (var tableKey in table.Keys)
{
String str_tableKey = (string)tableKey;
if (str_tableKey[0] == '.')
{
str_tableKey = str_tableKey.Substring(1);
}
SortedList list = (SortedList)table[tableKey].GetType().InvokeMember("m_list",
BindingFlags.NonPublic |
BindingFlags.GetField |
BindingFlags.Instance,
null,
table[tableKey],
new object[] { });
foreach (var listKey in list.Keys)
{
String url = "https://" + str_tableKey + (string)listKey;
cookieCollection.Add(cookieJar.GetCookies(new Uri(url)));
}
}
return cookieCollection;
}
不幸的是,此解决方法使用了一些我无法使用的 类,例如来自 System.Collections 命名空间的 Hashtable,因为我从事 Xamarin.Forms 便携式项目。
我发现有一个名为 Microsoft Base Class Library 的包,它似乎包含 System.Collections 命名空间,但即使在我的便携式项目上安装它之后,我不能使用 System.Collections 包。
我是不是遗漏了什么,还是我误解了什么?如果您知道我如何使用这些 Classes,请确切地告诉我我需要做什么。
基于 PCL 的 System.Collection
是 缺失的 大多数非通用集合,并且 Hashtable
是缺失的 类.
Hashtable
是一个非通用字典,因为它实现了 IDictionary,并且在功能上遵循 Dictionary<object, object>
,它在所有 PCL 配置文件中都可用,并且共享主要方法和属性Hashtable
.
因此只需将所有 Hashtable
实例替换为 Dictionary<TKey, TValue>
实例以静态键入您的 cookie 或 Dictionary<object, object>
如果您真的不关心键入。
所以,你好。
我对 .NET Framework 的 CookieContainer Class 有疑问。当我在 CookieContainer 中有 cookie 时,getCookies 无法获取 Cookies。 我认为问题出在 CookieContainer Class 的方法 getCookies() 上,因为我的域有很多子域,例如:
this.has.lots.of.subdomains.com
不知道,如果它是一个错误,或者如果我做错了什么,但我决定通过获取所有 Cookie 来解决这个问题,然后自己搜索 遍历我得到的所有 Cookie(大部分只有 2 - 3 个 Cookie,所以没什么大不了的。)
我在Whosebug上找到的方法是这个:
public static CookieCollection GetAllCookies(this CookieContainer cookieJar)
{
CookieCollection cookieCollection = new CookieCollection();
Hashtable table = (Hashtable)cookieJar.GetType().InvokeMember("m_domainTable",
BindingFlags.NonPublic |
BindingFlags.GetField |
BindingFlags.Instance,
null,
cookieJar,
new object[] { });
foreach (var tableKey in table.Keys)
{
String str_tableKey = (string)tableKey;
if (str_tableKey[0] == '.')
{
str_tableKey = str_tableKey.Substring(1);
}
SortedList list = (SortedList)table[tableKey].GetType().InvokeMember("m_list",
BindingFlags.NonPublic |
BindingFlags.GetField |
BindingFlags.Instance,
null,
table[tableKey],
new object[] { });
foreach (var listKey in list.Keys)
{
String url = "https://" + str_tableKey + (string)listKey;
cookieCollection.Add(cookieJar.GetCookies(new Uri(url)));
}
}
return cookieCollection;
}
不幸的是,此解决方法使用了一些我无法使用的 类,例如来自 System.Collections 命名空间的 Hashtable,因为我从事 Xamarin.Forms 便携式项目。
我发现有一个名为 Microsoft Base Class Library 的包,它似乎包含 System.Collections 命名空间,但即使在我的便携式项目上安装它之后,我不能使用 System.Collections 包。
我是不是遗漏了什么,还是我误解了什么?如果您知道我如何使用这些 Classes,请确切地告诉我我需要做什么。
基于 PCL 的 System.Collection
是 缺失的 大多数非通用集合,并且 Hashtable
是缺失的 类.
Hashtable
是一个非通用字典,因为它实现了 IDictionary,并且在功能上遵循 Dictionary<object, object>
,它在所有 PCL 配置文件中都可用,并且共享主要方法和属性Hashtable
.
因此只需将所有 Hashtable
实例替换为 Dictionary<TKey, TValue>
实例以静态键入您的 cookie 或 Dictionary<object, object>
如果您真的不关心键入。