简体   繁体   中英

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

So, hello there.

I have an issue with the CookieContainer Class of the .NET Framework . When i have cookies inside the CookieContainer, the Cookies cannot be obtained by getCookies. I think, the issue lies in the method getCookies() of the CookieContainer Class, because my domain has many subdomains like:

this.has.lots.of.subdomains.com

Dont know, if its a bug, or if i am doing something wrong, but i decided to work around this issue by obtaining all Cookies, and then search by myself with iterating through all Cookies i got (which are mostly just 2 - 3 Cookies, so its not quite the big deal.)

The Method I found on stackoverflow is this one:

 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;
    }

Unfortuneatly , this workaround uses some classes i cant use, like Hashtable from the System.Collections Namespace, because I'am working on a Xamarin.Forms Portable Project.

I found out, that there is a Package called Microsoft Base Class Library, which seems to include the System.Collections Namespace, but even after installing it on my portable project, i cant use the System.Collections Package.

Am I missing something, or did i misunderstand something? If you know, how i can use these Classes, please tell me exactly what i need to do.

The PCL-based System.Collection are missing most of the non-generic collections and Hashtable is one of the missing classes.

Hashtable is a non-generic dictionary as it implements IDictionary, and follows Dictionary<object, object> in function, which is available across all PCL profiles and shares the major methods and properties as Hashtable .

So just replace all your Hashtable instances with Dictionary<TKey, TValue> instances to statically type your cookies or Dictionary<object, object> if you really do not care about the typing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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