I work on a project target WP 7.5 and above, I add a packages using the Nuget in VS 2012 which name is Coding4fun(Controls).
After that, the IDE give me a warning,
Warning 1 The predefined type 'System.Tuple' is defined in multiple
assemblies in the global alias; using definition from
'c:\Users\Gao\Documents\Visual Studio
2012\Projects\TFSGit\xicihutong\packages\Coding4Fun.Toolkit.Controls.2.0.5\lib\wp71\Coding4Fun.Toolkit.Controls.dll' C:\Users\Gao\Documents\Visual
Studio 2012\Projects\TFSGit\xicihutong\xicihutong\CSC xicihutong
The app runs OK, but how to solve this warning?
From Compiler Warning (level 1) CS1685
This error occurs when a predefined system type such as System.Tuple
is found in two assemblies. One way this can happen is if you are
referencing mscorlib from two different places, such as trying to run
the.Net Framework versions 1.0 and 1.1 side-by-side.
The compiler will use the definition from only one of the assemblies.
This problem occurs probably System.Tuple is defined in two different assemblies. Probably your Coding4Fun.Toolkit.Controls.dll has too besides mscorlib.dll.
If you want to look them both you can check them;
Get a decompiler like ILSpy or Jetbrains dotPeek.
Add all 3rd party assemblies.
Search for System.Tuple
But how to solve this warning?
You can define your 3rd party assemblies reference some aliases. You can follow in your project
Reference -> Properties -> Aliases ->
And change "global" to something different.
Check out for information extern alias (C# Reference)
Newer version of c4f toolkit has resolved this issue.
Related
For learning purposes, I use csc.exe myprogram.cs shipped with Visual Studio Community (my version 17.3.4) to compile basic C# programs. It mostly works, except when using what appears to be latest language features, for example, array range shorthand array[0..] or element from end shothand array[^1].
For example, when trying to access [^1] element.
Arrays.cs(58,47): error CS0518: Predefined type 'System.Index' is not defined or imported
Arrays.cs(58,47): error CS0656: Missing compiler required member 'System.Index..ctor'
I tried passing -langversion:preview to csc but still not working. Also I was unable to find proper usings for it to work.
Can I somehow get those features to work with basic csc compilation? They worked when I created csproj file and used dotnet build, but doing so was multiple times slower than using just csc. Thanks!
Arrays.cs(58,47): error CS0518: Predefined type 'System.Index' is not defined or imported
Arrays.cs(58,47): error CS0656: Missing compiler required member 'System.Index..ctor'
The compiler uses several types to lower index / range expressions to IL. One of those types is System.Index. This error is the compiler noting that it cannot find that type which is necessary to lower that expression to IL.
This feature was added as a part of netcoreapp3.1 where csc defaults to compiling for .NET Framework applications. These types are not present in the standard set of references you get for .NET Framework hence this is why you get the error.
They worked when I created csproj file and used dotnet build ...
That worked because your project file contained something like the following:
<TargetFramework>net6.0</TargetFramework>
This targeted your application for .NET Core, the build command passed along the standard references for that and those included the System.Index type.
... but doing so was multiple times slower than using just csc.
Building a .NET application involves more than just compiling code. It is finding the correct set of references, building dependencies, compiling and deploying the resulting binaries. It's more work hence it's going to take longer to complete. Multiple times slower is not expected, particularly for repeated executions, but it will take more time that invoking csc, or any ofeth other tools used in build, directly.
In my Xamarin project I'm using ReactiveUI and Firebase.Xamarin. When these two libraries are being used at once, any attempt to use System.Reactive.Linq.Observable extension methods such as Where or Select results in compile time error:
The call is ambiguous between the following methods or properties: 'System.Reactive.Linq.Observable.Where(System.IObservable, System.Func)' and 'System.Reactive.Linq.Observable.Where(System.IObservable, System.Func)'
I presume this is being caused by both of these libraries defining/referencing the same methods in same namespaces. How can I fix that?
It is likely that you are loading more than one version of the assembly that defines that namespace. It may be that the assemblies that you refer to load a common dependency. If they load different versions of that assembly you would get this error.
Ensure that you are using versions of the two libraries that depend on the same version of the dependant assembly.
Firebase.Xamarin is no longer maintained so if I wanted to follow #PhilipSmith advice I would have to use ReactiveUI version that is over 2 years old. I didn't wanted to do it so I decided to clone Firebase.Xamarin since its OSS and add it to my solution as another project with intention to alter the ReactiveUI version it was referencing. But when I added is as a referenced project the error disappeared.
I have a project that depends on an external program, this external program has an API, well actually it has about 17 different APIs all slightly different for version 2000-2017. Now within these 17 versions I want to support about 5 of them. (2012-2017) but there are several features that were in 2012 that have since been renamed in 2017.
Now the good news is that I can trivially determine which version of the program a given user is using and any shared functions (90%+) can be called using a reference to a different version of the API. However I need some of the remaining 10% of the features. So I need to include the references to multiple APIs so that my program will compile and then at runtime pick which version it receives.
Now what I tried is to go into the visual studio (2015 community version), and add a reference to several of thes. However the moment I try adding in a second reference I get a error message: a reference to [API.dll] could not be added a reference to the component [API.dll] already exists in project..
I would like the method use to be such that if a function with a given name exists in one of the versions it should bind to that one and if a given function name exists in multiple APIs then it should bind to the latest. Any idea how to do this? Maybe something using the extern alias keyword?
I looked at How to reference two versions of an API? and the accepted answer won' t work but the second answer might, anyone capable of explaining if that one will work and if so how to do it correctly?
Basically, it is not allowed to add multiple references with the same name.
If you are the assembly owner, you have to change the file name in the manifest to generate DLLs with different names.
You can also manage the assembly version in the configuration file or load at runtime.
My suggestion is to merge all the DLLs into a single file. You can use ILMerge to do this.
I have an error, when I try to build my .net 4, c# project. Everything works great, but when I add an external reference to a given DLL, it stops working, it can't build, throws this type of some errors:
Error 36 The type 'System.Tuple' exists in both 'C:\Program Files\Reference Assemblies\Microsoft\Framework.NETFramework\v4.0\mscorlib.dll' and 'C:\Projects\Project1\ExternalRefernces\SharpSNMP\SharpSnmpLib.dll' C:\Projects\Project1\CheckerStore.cs 17 21
Note, I did not do anything with the new library, just added as a reference. Any ideas?
You can solve this problem by specifying an external alias. Select the SharpSNMP reference in your project. In the properties window change Aliases from global to say SharpSNMP. In your code type this
extern alias SharpSNMP;
...
System.Tuple<T1,T2> sysTulpe;
SharpSNMP::System.Tuple<T1,T2> sharpTulpe;
or
extern alias SharpSNMP;
using SharpSystem = SharpSNMP::System;
...
System.Tuple<T1,T2> sysTulpe;
SharpSystem.Tuple<T1,T2> sharpTulpe;
See Aliases: overcoming name conflicts part 2: extern alias
What you can do is either change the target version to 3.5 or make some changes in the SharpSNMPLib. The source can be fetched from here or here.
The changes you need to make is specifically moving the System.Tuple type somewhere else.
Edit:
I belive you have added a reference to a precomplied DLL. A DLL that is NOT compiled for framework version 4. What you need to do is download the source code (see links above) and compile the project with target version 4.
Why you need to do this is because there are conditional build parameters depending on the framework target version. The SharpSNMPLib System.Tuple is used for version <= 3.5 and the framework System.Tuple is used for version >= 4.
Edit:
Reproduced your problem using framework System.Tuple and SharpSNMPLib.dll.
Successfully built SharpSNMPLib targeted on version 4.
Successfully built application using framework System.Tuple and the new SharpSNMPLib.dll.
Simply go to your CheckerStore.cs file, line 1721 (if I'm right). Find the Tuple class, and reference it using it's fully qualified name.
The library for some reason re-implements some system types. Likely reason is to make code to be source level compatible when using older versions of the framework.
Most likely there is a version of this SharpSNMP library that works with 4.0 framework. Check if you already have correct on in your source tree. Check with creators of the library what versions of the assembly you need to use with given framework version and what is recommended way of doing it.
Since version 3.0, .NET installs a bunch of different 'reference assemblies' under C:\Program Files\Reference Assemblies\Microsoft...., to support different profiles (say .NET 3.5 client profile, Silverlight profile). Each of these is a proper .NET assembly that contains only metadata - no IL code - and each assembly is marked with the ReferenceAssemblyAttribute. The metadata is restricted to those types and member available under the applicable profile - that's how intellisense shows a restricted set of types and members. The reference assemblies are not used at runtime.
I learnt a bit about it from this blog post.
I'd like to create and use such a reference assembly for my library.
How do I create a metadata-only assembly - is there some compiler flag or ildasm post-processor?
Are there attributes that control which types are exported to different 'profiles'?
How does the reference assembly resolution at runtime - if I had the reference assembly present in my application directory instead of the 'real' assembly, and not in the GAC at all, would probing continue and my AssemblyResolve event fire so that I can supply the actual assembly at runtime?
Any ideas or pointers to where I could learn more about this would be greatly appreciated.
Update: Looking around a bit, I see the .NET 3.0 'reference assemblies' do seem to have some code, and the Reference Assembly attribute was only added in .NET 4.0. So the behaviour might have changed a bit with the new runtime.
Why? For my Excel-DNA ( http://exceldna.codeplex.com ) add-in library, I create single-file .xll add-in by packing the referenced assemblies into the .xll file as resources. The packed assemblies include the user's add-in code, as well as the Excel-DNA managed library (which might be referenced by the user's assembly).
It sounds rather complicated, but works wonderfully well most of the time - the add-in is a single small file, so no installation of distribution issues. I run into (not unexpected) problems because of different versions - if there is an old version of the Excel-DNA managed library as a file, the runtime will load that instead of the packed one (I never get a chance to interfere with the loading).
I hope to make a reference assembly for my Excel-DNA managed part that users can point to when compiling their add-ins. But if they mistakenly have a version of this assembly at runtime, the runtime should fail to load it, and give me a chance to load the real assembly from resources.
To create a reference assembly, you would add this line to your AssemblyInfo.cs file:
[assembly: ReferenceAssembly]
To load others, you can reference them as usual from your VisualStudio project references, or dynamically at runtime using:
Assembly.ReflectionOnlyLoad()
or
Assembly.ReflectionOnlyLoadFrom()
If you have added a reference to a metadata/reference assembly using VisualStudio, then intellisense and building your project will work just fine, however if you try to execute your application against one, you will get an error:
System.BadImageFormatException: Cannot load a reference assembly for execution.
So the expectation is that at runtime you would substitute in a real assembly that has the same metadata signature.
If you have loaded an assembly dynamically with Assembly.ReflectionOnlyLoad() then you can only do all the reflection operations against it (read the types, methods, properties, attributes, etc, but can not dynamically invoke any of them).
I am curious as to what your use case is for creating a metadata-only assembly. I've never had to do that before, and would love to know if you have found some interesting use for them...
If you are still interested in this possibility, I've made a fork of the il-repack project based on Mono.Cecil which accepts a "/meta" command line argument to generate a metadata only assembly for the public and protected types.
https://github.com/KarimLUCCIN/il-repack/tree/xna
(I tried it on the full XNA Framework and its working afaik ...)
Yes, this is new for .NET 4.0. I'm fairly sure this was done to avoid the nasty versioning problems in the .NET 2.0 service packs. Best example is the WaitHandle.WaitOne(int) overload, added and documented in SP2. A popular overload because it avoids having to guess at the proper value for *exitContext" in the WaitOne(int, bool) overload. Problem is, the program bombs when it is run on a version of 2.0 that's older than SP2. Not a happy diagnostic either. Isolating the reference assemblies ensures that this can't happen again.
I think those reference assemblies were created by starting from a copy of the compiled assemblies (like it was done in previous versions) and running them through a tool that strips the IL from the assembly. That tool is however not available to us, nothing in the bin/netfx 4.0 tools Windows 7.1 SDK subdirectory that could do this. Not exactly a tool that gets used often so it is probably not production quality :)
You might have luck with the Cecil Library (from Mono); I think the implementation allows ILMerge functionality, it might just as well write metadata only assemblies.
I have scanned the code base (documentation is sparse), but haven't found any obvious clues yet...
YYMV