繁体   English   中英

如何在Xamarin.Forms可移植项目中使用System.Collections

[英]How to use System.Collections in a Xamarin.Forms Portable Project

所以,你好。

.NET FrameworkCookieContainer类出现问题。 当我在CookieContainer中包含cookie时,getCookies无法获得 Cookies 我认为,问题出在CookieContainer类的getCookies()方法中,因为我的域有很多子域,例如:

this.has.lots.of.subdomains.com

不知道,如果它是一个错误,或者我做错了什么,但是我决定通过获取所有Cookies来解决此问题,然后自己遍历我得到的所有Cookies进行搜索(通常只有2-3个Cookies) ,所以它并不是什么大问题。)

我在stackoverflow上发现的方法是这样的:

 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程序包。

我是否缺少某些东西,或者我误会了一些东西? 如果您知道我如何使用这些类,请准确告诉我我需要做什么。

基于PCL的System.Collection 缺少大多数非泛型集合,而Hashtable是缺少的类之一。

Hashtable是一个非通用词典,因为它实现了IDictionary,并且在功能上遵循Dictionary<object, object> ,可在所有PCL概要文件中使用,并与Hashtable共享主要方法和属性。

因此Dictionary<object, object>如果您真的不在乎键入Dictionary<TKey, TValue>只需将所有Hashtable实例替换为Dictionary<TKey, TValue>实例以静态键入cookie或Dictionary<object, object>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM