Reference Collections.Immutable in visual studio extension - c#

I have a visual studio extension (for VS 2017) that uses Roslyn in some functions and references System.Collections.Immutable, Version=1.2.2.0. When I first ran it, it would complain that it didn't have a reference to the System.Collections.Immutable assembly, so I added it the assets in the source.extensions.vsixmanifest (along with Microsoft.CodeAnalysis.CSharp.dll and Microsoft.CodeAnalysis.dll as it happens).
This fixed my problem with the missing assembly, but now I get:
Method not found: 'System.Collections.Immutable.ImmutableArray`1 Microsoft.CodeAnalysis.Scripting.Script.Compile(System.Threading.CancellationToken)'.
I believe it might still be referencing a different version of System.Collections.Immutable, while debugging the extension, if I try this:
System.Reflection.Assembly.GetAssembly(typeof(System.Collections.Immutable.ImmutableArray))
I get:
error CS0433: The type 'ImmutableArray' exists in both 'System.Collections.Immutable, Version=1.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' and 'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
Is there a way to resolve this within the extension? (things like .config assembly redirects don't really work well unless you edit Visual Studio's own .config, which I 'd prefer to avoid)

Related

What Target Framework and References to use when compiling a DLL for Unity

I have some scripts which I use normally in Unity 2019.4 and also compile into a separate DLL to distribute without the source code. Everything was working until I started using System.Array.Empty<T>() in a few places which works fine in Unity but is preventing me from compiling the separate DLL.
Originally, I had the Target Framework set to Unity 3.5 .net full Base Class Libraries, but that's now giving me the error: 'Array' does not contain a definition for 'Empty'
Apparently Array.Empty was added in .NET Framework 4.6, but if I set the Target Framework to that (or later) I get: The type 'ISet<>' is defined in an assembly that is not referenced. You must add a reference to assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' from everywhere I use a HashSet<T> (even though I'm not using ISet<> directly).
If I manually add a reference to the System.dll from the Unity install directory, it gives the exact same error but says ISet<> is in the netstandard dll now.
Adding a reference to netstandard.dll from the Unity install directory gives Predefined type 'System.Object' is not defined or imported.
Trying to fix that by referencing mscorlib.dll gives The type 'HashSet<T>' exists in both 'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' and 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
So what Target Framework and References am I supposed to be using to compile a DLL for Unity 2019.4?
I fixed the issue by copying one of the csproj files that Unity generates for an Assembly Definition and removing the stuff I don't need.
The main difference seems to be the inclusion of this block:
<PropertyGroup>
<NoConfig>true</NoConfig>
<NoStdLib>true</NoStdLib>
<AddAdditionalExplicitAssemblyReferences>false</AddAdditionalExplicitAssemblyReferences>
<ImplicitlyExpandNETStandardFacades>false</ImplicitlyExpandNETStandardFacades>
<ImplicitlyExpandDesignTimeFacades>false</ImplicitlyExpandDesignTimeFacades>
</PropertyGroup>
Which I'm guessing allows it to also define its own references to mscorlib.dll, netstandard.dll, and System.Core.dll without having them conflict with the ones imported by the framework.

Failed to build UWP project with Visual Studio 2017 due to missing reference

I am trying to build a UWP project using Visual Studio 2017.
Without success :(
Here is the error messages content:
error CS0012: The type 'IAsyncOperationWithProgress<,>' is defined in an assembly that is not referenced. You must add a reference to assembly 'Windows, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime'.
error CS0012: The type 'IAsyncActionWithProgress<>' is defined in an assembly that is not referenced. You must add a reference to assembly 'Windows, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime'.
error CS0012: The type 'IAsyncOperation<>' is defined in an assembly that is not referenced. You must add a reference to assembly 'Windows, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime'.
error CS0012: The type 'IAsyncAction' is defined in an assembly that is not referenced. You must add a reference to assembly 'Windows, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime'.
error CS0103: The name 'Package' does not exist in the current context
The code lines inducing the errors all contain the await keyword and are like that:
var storageFolder = Task.Run(async () => await StorageFolder.GetFolderFromPathAsync(folderPath));
The "Quick actions" propose me to Add reference to 'Windows, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime'.
When I try, I have, in the Reference Manager' dialog :
The Universal SDK is already referenced. Please us the Object Browser to explore the references in th Universal Windows SDK.
And
No Framework assemblies were found on the machine
I un-installed VS and re-install => nothing changed
I un-installed all features/applications referencing .net and visual Studio and re-install VS => nothing changed
I tried installing the Windows 10 SDK (10.0.14393) before or after installing VS => nothing changed
I really have no idea of what goes wrong with my dev environment.
Do you know what the error messages mean ?
And does someone can help me to fix it?
TIA
I didn't know how I fixed the problem.
I "played" with the emulator extensions, especially the Android one.
Without understanding, while uninstalling it, my problem disappears.
Sorry, for the ones who will encounter the same problem I did, I close the discussion as my problem has been fixed, even if I can't explain why.

References to Microsoft.Bcl patched libraries (System.Runtime, etc) are not added during build on some machines

We recently added Microsoft.Bcl.Async to our Solution using NuGet.
This works totally fine on two machines. However, on two different machines, the compiled assemblies lack references to the patched .NET assemblies that are shipped with Microsoft.Bcl (System.Runtime, System.IO, etc.). All machines run Visual Studio 2015. The references are visible in the project files and in Visual Studio, they just don't get compiled into the assemblies (I wrote a small tool for that, querying the result from Assembly.GetReferencedAssemblies.
If I run the binaries compiled by one of the 'broken' computers on Windows XP, it doesn't work, because Microsoft.Bcl isn't actually included.
We've already tried getting a fresh copy in a new workspace and even reinstalling Visual Studio, but the build is still broken.
EDIT:
I found the following lines in the build output:
16>CSC : warning CS1685: The predefined type 'INotifyCompletion' is defined in multiple assemblies in the global alias; using definition from 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
16>CSC : warning CS1685: The predefined type 'IAsyncStateMachine' is defined in multiple assemblies in the global alias; using definition from 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
16>CSC : warning CS1685: The predefined type 'AsyncTaskMethodBuilder<TResult>' is defined in multiple assemblies in the global alias; using definition from 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
16>CSC : warning CS1685: The predefined type 'ICriticalNotifyCompletion' is defined in multiple assemblies in the global alias; using definition from 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
16>CSC : warning CS1685: The predefined type 'AsyncTaskMethodBuilder' is defined in multiple assemblies in the global alias; using definition from 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
I am not quite sure yet how to resolve this, so help is appreciated, but I think, this might be the problem I am facing.
Well, I guess the problem resolved itself. "Broken" computer 1 apparently wasn't broken or was and magically isn't anymore and computer 2 is now fixed, after a wild ride of uninstalling everything .NET related and reinstalling Visual Studio.
It is kind of disappointing that I am now not able to properly diagnose the source of this problem, but I will just have to live with that.

ObservableCollection<T> exists in both 'System.Windows' and 'System'

After creating a new WPF project in VS 2017 targeting .NET 4.6 as soon as I try to use ObservableCollection<T> in my code I get the following error which prevents the app from building:
ObservableCollection exists in both 'System.Windows' and 'System'
In my projects I created in VS 2015 I have referenced both of these assemblies and the application worked just fine. I tried deleting contents of app's bin and obj folder, restarting VS and performing Build > Clean Solution but that did not help.
Removing reference to System.Windows allows application to build but that adversely affects other code (for example RaiseCanExecuteChanged of my DelegateCommand requires a reference to System.Windows).
Any idea why this is happening and how to fix it?
UPDATE Here is the full error message:
Severity Code Description Project File Line Suppression State
Error CS0433 The type 'ObservableCollection' exists in both
'System.Windows, Version=2.0.5.0, Culture=neutral,
PublicKeyToken=7cec85d7bea7798e, Retargetable=Yes' and 'System,
Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
The problem was that ReSharper was adding a reference to:
C:\Program Files (x86)\Reference
Assemblies\Microsoft\Framework.NETPortable\v4.0\Profile\Profile158\System.Windows.dll
instead of:
C:\Program Files (x86)\Reference
Assemblies\Microsoft\Framework.NETFramework\v4.6.2\System.Windows.dll
Once I removed the reference added by ReSharper and manually added the correct reference the problem was resolved.
ReSharper was detecting that DelegateCommand implementation of ICommand used by my MVVM framework needed a reference to System.Windows but was erroneusly adding a reference to System.Windows found in the NETPortable instead of System.Windows found in .NET...

Error trying to use WordsSegmenter.GetTokens()

The code:
WordsSegmenter s = new WordsSegmenter("en-US");
var words = s.GetTokens("The price of this API is €10,000.99.");
gives me a build error: "Error 2 The type 'System.Collections.Generic.IReadOnlyList`1' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
I have System.Collections.Generic referenced and suspect that the error above is disguising the real problem.
I'm building this in Visual Studio Express for Windows Desktop. Do I perhaps have to use VS Express for Windows?
Changing the target Framework of my project to other 4.x versions doesn't help.
Two actions fixed this: Opening my project.csproj file in a text editor and changing the TargetPlatformVersion node to "8.1". Then added references to Windows.winmd and Windows.Runtime.WindowsRuntime.dll.
Kudos to Xavier H at Intel for this post: https://software.intel.com/en-us/articles/using-winrt-apis-from-desktop-applications

Categories