简体   繁体   中英

'System.MissingMethodException' exception in mscorlib.dll

I am getting an exception when debugging my program.

Exception thrown: 'System.MissingMethodException' in mscorlib.dll System.Globalization.CultureInfo

"Constructor on type 'System.Globalization.CultureInfo' not found."

at my

xmlns:global="clr-namespace:System.Globalization;assembly=mscorlib"
xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
xmlns:dat="clr-namespace:System.Windows.Data;assembly=PresentationFramework"
>
<Window.Resources>
    <ObjectDataProvider x:Key="CulturesProvider"
                        ObjectType="{x:Type global:CultureInfo}" 
                        MethodName="GetCultures">
        <ObjectDataProvider.MethodParameters>
            <global:CultureTypes>AllCultures</global:CultureTypes>
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>

    <CollectionViewSource x:Key="MyCVS"
                          Source="{StaticResource CulturesProvider}">
        <CollectionViewSource.SortDescriptions>
            <scm:SortDescription PropertyName="IetfLanguageTag" />
        </CollectionViewSource.SortDescriptions>
        <CollectionViewSource.GroupDescriptions>
            <dat:PropertyGroupDescription PropertyName="Parent" />
        </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource>

Are there any references that i need to import to make this work?

The code you posted works fine, I tried it bound to a ListBox and the different cultures were displayed properly.

Although I noticed the exception you described in the Output window, so I started to investigate.

Here you can find the source code of the ObjectDataProvider . I linked to the part which actually interests us.

Here you can see that the code will actually try to create an instance of the ObjectType you provided even if you are only trying to call a static method on that type. This will fail with the above exception, because CultureInfo doesn't have a constructor which takes zero parameters.

This is even mentioned in the comment here :

// if InvokeMethod failed, we prefer to surface the instantiation error, if any.
// (although this can be confusing if the user wanted to call a static method)

Also I found this forum post where the accepted answer to the same question is the following:

If your application works well, you can ignore the output. This output is used for debugging the data binding errors. But, generally even your code works well, the output may also appears, so you can ignore it.

So I'd say you can ignore this error if your application works and as I tested it, it should work.

All that said, if you are really annoyed by the exception, you can fix it by doing the following:

xmlns:sys="clr-namespace:System;assembly=mscorlib"
>
<Window.Resources>

    <ObjectDataProvider x:Key="CulturesProvider" ObjectType="{x:Type global:CultureInfo}" MethodName="GetCultures">
        <ObjectDataProvider.ConstructorParameters>
            <x:Static Member="sys:String.Empty" />
        </ObjectDataProvider.ConstructorParameters>
        <ObjectDataProvider.MethodParameters>
            <global:CultureTypes>AllCultures</global:CultureTypes>
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>

This won't throw the exception, because creating an instance of CultureInfo in the ObjectDataProvider 's code will succeed. It will use the constructor which takes one string parameter.

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