How can I avoid this InvalidCastException?

I have an extension method that I've been using for years in WinForms, but not since trying to use it in a new WPF project. Method:

public static String GetDescription(this Enum value)
{
    //var info = value.GetType().GetField(value.ToString());
    //if (info != null)
    //{
    //    var attributes = (DescriptionAttribute[])info.GetCustomAttributes(typeof(DescriptionAttribute), false);
    //    if (attributes != null && attributes.Length > 0)
    //        return attributes[0].Description;
    //}
    //return value.ToString();

    var info = value.GetType().GetField(value.ToString());
    var attributes = Attribute.GetCustomAttributes(info);
    if (attributes.Length > 0 && (attributes[0] is System.ComponentModel.DescriptionAttribute))
        return ((System.ComponentModel.DescriptionAttribute)attributes[0]).Description;
    return value.ToString();
}

      

The first snippet (which is commented out) is the original method. The second piece is a slightly different version that I tested. If I force the return string with an executable piece, I get this exception:

InvalidCastException image

Full text of the exception from this dialog:

Additional information: [A] System.ComponentModel.DescriptionAttribute cannot be translated into [B] System.ComponentModel.DescriptionAttribute. Type A is taken from "System.ComponentModel.Primitives, Version = 4.1.1.0, Culture = neutral, PublicKeyToken = b03f5f7f11d50a3a" in the "Default" context in the location "C: \ TFS_Local \ Antero \ AnteroWPF \ bin \ Debug \ System.ComponentModel .Primitives.dll. Type B starts with "System, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089" in the context "Default" in location "C: \ WINDOWS \ Microsoft.Net \ assembly \ GAC_MSIL \ System \ v4.0_4.0.0.0__b77a5c561934e089 \ System.dll ".

There seems to be two assembled assemblies:

System.ComponentModel.Primitives.dll

(exists in the bin build directory ... not really sure how it got there)

System.dll

(directly from the link from the GAC, for obvious reasons)

I am completely lost here. If I remove the ComponentModel DLL it becomes an exception. Although the exception claims to System

have a type, it is not possible to use it. That is System.ComponentModel.DescriptionAttribute

, it doesn't seem to make sense to do this in the absence of a corresponding DLL.

So, if I delete one and it fails and the other becomes unusable ... then why is this exception even happening ?!

EDIT: I think it costs nothing, if I traverse the value attributes

in memory, I see that the array has one element and is of type System.ComponentModel.DescriptionAttribute

.

+3


source to share


2 answers


If I remove DLL ComponentModel it becomes an exception

What will be the exception?

The exception that you get 100% clear: two different assemblies that link to your project, define one and the same type System.ComponentModel.DescriptionAttribute

. This type is documented as defined in System.dll . Thus, the assembly System.ComponentModel.Primitives.dll appears to be suspicious.

A little internet search brings me to this System.ComponentModel.Primitives NuGet package . From this description, it appears to be related to .NET Core, and indeed there is an implementation of this type in the .NET Core source code .



At compile time, the assembly appears to have been placed in your build directory C:\TFS_Local\Antero\AnteroWPF\bin\Debug

. So the question is, if you are building a WPF program, why do you have a .NET Core assembly in your build directory? Have you installed the NuGet package at some point? The build directory has a root TFS_Local

hinting that you may be credited to the Team Foundation Server repository. Is it possible a co-worker has installed a package for your build?

I haven't seen anything that would lead me to believe that you can mix and match the desktop .NET Framework with .NET Core. So the short answer seems to me to be simply "don't do it." You may get another error without referencing System.ComponentModel.Primitives.dll , but you will have to address it differently. I don't know of any reason to believe that you should expect to be able to compile a WPF program on the desktop using a .NET Core DLL.

If you think otherwise, it would be helpful if you could explain why you think you can use a .NET Core assembly in your WPF program, how you do it, and what steps you took to ensure that types are declared in. NET Core, do not conflict with the types declared in the .NET Framework collections, including System.dll .

+2


source


To fix this, put the bin and obj folders. Set a copy of local to false on your links from the GAC.



0


source







All Articles