[Mono-bugs] [Bug 643379] New: NameValueConfigurationCollection.AllKeys uses contravariant array cast

bugzilla_noreply at novell.com bugzilla_noreply at novell.com
Sun Oct 3 06:16:00 EDT 2010


https://bugzilla.novell.com/show_bug.cgi?id=643379

https://bugzilla.novell.com/show_bug.cgi?id=643379#c0


           Summary: NameValueConfigurationCollection.AllKeys uses
                    contravariant array cast
    Classification: Mono
           Product: Mono: Class Libraries
           Version: SVN
          Platform: x86-64
        OS/Version: Ubuntu
            Status: NEW
          Severity: Normal
          Priority: P5 - None
         Component: System
        AssignedTo: mono-bugs at lists.ximian.com
        ReportedBy: Novell at meinersbur.de
         QAContact: mono-bugs at lists.ximian.com
          Found By: ---
           Blocker: ---


User-Agent:       Mozilla/5.0 (X11; U; Linux x86_64; de; rv:1.9.2.10)
Gecko/20100915 Ubuntu/10.04 (lucid) Firefox/3.6.10

The property System.Configuration.NameValueConfigurationCollection.AllKeys
always throws InvalidCastException.

Example program:

using System.Configuration;
class Test {
    public static void Main(string[] arg) {
        var conf = new NameValueConfigurationCollection();
        var dummy = conf.AllKeys;
    }
}

Reproducible: Always

Steps to Reproduce:
1. gmcs test.cs -r:System.Configuration
2. mono Test.exe
Actual Results:  
System.InvalidCastException: Cannot cast from source type to destination type.


Expected Results:  
Program ends without exception

Error is in NameValueConfigurationCollection.cs:

        public string[] AllKeys {
            get {
                return (string[]) BaseGetAllKeys();
            }
        }

where BaseGetAllKeys() returns an object[]. Since arrays in C# are covariant,
but not contravariant, this throws an InvalidCastException. Using latest git
checkout (2nd Oct) on Ubuntu Lucid.

Changing to 

        public string[] AllKeys {
            get {
                object[] objArr = BaseGetAllKeys();
                string[] result = new string[objArr.Length];
                for (int i=0; i<objArr.Length-1; i++)
                    result[i] = objArr[i].ToString();
                return result;
            }
        }

yields the expected result. This might not be the best implementation.

-- 
Configure bugmail: https://bugzilla.novell.com/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the QA contact for the bug.
You are the assignee for the bug.


More information about the mono-bugs mailing list