Feofilakt Feofilakt - 2 months ago 6
C# Question

Error "Type does not exist in the namespace" after using of external library in this type

It seems like there is tens of posts in the internet with such error. But I think my case is different, and all solutions I found do not help me.
I have type (I deleted all unimportant things but it changes nothing):

using ExternalLibrary;
namespace MyProject
{
public class MyClass
{
public ExternalLibrary.ExternalType myVar;
}
}


I'm trying to get access to this type in the XAML for further using it as a DataType:

<Window x:Class="MyProject.MyWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyProject">
<Window.Resources>
<local:MyClass x:Key="myKey"/>
</Window.Resources>
</Window>


and get error. If delete
using ExternalLibrary
and
myVar
from
MyClass
, all works. Use external library in class - and class does not exist in namespace. Delete the using of library - and class exist again. I use VisualStudio 2015, I had tried VS 2013 and error did not gone. I tried rebuild in debug, release, x86, x64, other frameworks, client profiles, different combines of namespaces and many other things. Is there solution of this problem? Thanks.

Answer

As it turned out the ExternalLibrary in question was built for AMD64, while the project using it was targeting any CPU. The compiler couldn't find the class when building the project for 32 bit processors, hence the error. You can safety switch to targeting 64 bit processors, since it won't work on other machines anyway. To switch the target processor go Build -> Configuration Manager -> Platform -> x64

You may want to choose a different combination of platform for each of your projects depending on active solution platform. For example, if your solution is targeting x86, project A builds for x86, while project B for any CPU, but if the solution's targeting x64, both projects A and B build for x64. For more information see Understanding Build Configuration.

If you're playing with these settings you may end up with an unexpected configuration, so it may make sense to have a look at what each project's targeting (you can do it in the Configuration Manager).