Why won't Visual Studio compile my C# file to a DLL? - c#

I have an old Unity project where I build a couple of (c#)editor-scripts into a dll years ago.
I am still using the dll a lot, but now I made some changes to the original scripts, and so I want to compile them into a dll again.
I don't know much about dll's and compiling and stuff like that, but luckily I wrote down exactly what I had to do to compile it into a dll, which is the following lines that I enter into Visual Studio 2010 command prompt:
csc /target:library /out:MyDLL.DLL /reference:"C:\Program Files (x86)\Unity_3.5.1f2\Editor\Data\Managed\UnityEngine.dll" MyEditorScript.cs
However when I do that, I get the error: "error CS0246: The type or namespace name `UnityEditor' could not be found. Are you missing a using directive or an assembly reference?"
I'm using the exact same (old) Unity version as I did back then, as well as the same Visual Studio (I do actually have and use new versions of both Visual Studio and Unity, here it just seemed safer to stick to what worked before), so I really don't understand why it doesn't work anymore.
I mean, I understand it needs the UnityEditor dll, but how come it worked before then?
There is a UnityEditor.dll in the same location as the UnityEngine.dll I reference in the command prompt, but when I try to add that (like this: csc /target:library /out:MyDLL.DLL /reference:"C:\Program Files (x86)\Unity_3.5.1f2\Editor\Data\Managed\UnityEngine.dll" "C:\Program Files (x86)\Unity_3.5.1f2\Editor\Data\Managed\UnityEditor.dll" MyEditorScript.cs) It complains that the dll is a binary for some reason.
Does anyone know how to fix this?

Apparently I just need another /reference: for the UnityEditor.dll as well, so it's like this: csc /target:library /out:MyDLL.DLL /reference:"C:\Program Files (x86)\Unity_3.5.1f2\Editor\Data\Managed\UnityEngine.dll" /reference:"C:\Program Files (x86)\Unity_3.5.1f2\Editor\Data\Managed\UnityEditor.dll" MyEditorScript.cs
I still don't get why I didn't need to reference the editor dll in the past though.
If anyone knows, feel free to shed some light on it

Related

Unable to compile C# code on Mac

I am receiving an error when I try to compile my c# source code file on my Mac. Normally I compile my source code using the mcs command on a bash terminal window and it compiles fine. But, this one particular project requires the installation of the NuGet package 'MySql.Data'. This has resulted in the following error message:
error CS0246: The type or namespace name 'MySqlConnection' could not be found. Are you missing an assembly reference?
I have tried to follow numerous solutions online to try and add the assembly reference, but my IDE just does not look like the help documentation's screenshots.
I have tried to Edit References, but no references appear. I tried to manually download the MySql.Data.dll (v.6.3.5.0) but I think it is for an outdated version of the MySql.Data class library as it does not work. Even after cleaning the code and rebuilding it.
Edit: I have also tried to add the dll that was included in the project folder but it said it could not locate the assembly (even though they are both in the same project folder).
Could not resolve this reference. Could not locate the assembly "search_feature". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors. (MSB3245) (search_feature)
Edit 2: I renamed the dll file from the project bin folder to MySql.Data.dll and I no longer get the error. But it still won't compile.
The code works fine when run in Visual Studios for Mac, but I just can't compile it. Any ideas how to add the assembly reference?
I am using the most up-to-date version of Visual Studios:
Visual Studios for Mac Community 7.5.4 (build 3)
I have tried uninstalling and reinstalling Visual Studios for Mac.
My code has the library reference at the top: Using MySql.Data.MySqlclient; and the project runs. It just won't compile.

How to fix "The "GetDependsOnNETStandard" task could not be loaded from the assembly"?

Yesterday I updated my visual studio 2017 from version 15.6.4 to 15.6.6 and then this error occured when i tried to run my program. Help me fix this please, i have no idea what to do. Please provide step by step procedure, thank you.
The "GetDependsOnNETStandard" task could not be loaded from the assembly C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\Microsoft\Microsoft.NET.Build.Extensions\tools\net46\Microsoft.NET.Build.Extensions.Tasks.dll. Could not load file or assembly 'file:///C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\Microsoft\Microsoft.NET.Build.Extensions\tools\net46\Microsoft.NET.Build.Extensions.Tasks.dll' or one of its dependencies. The system cannot find the file specified. Confirm that the declaration is correct, that the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask.
Try to execute the command: "git clean -fdx" to clean up some (possible) trash file on your git repo.
Works for me.
I had the same error.
In my case, the error occurred because the one of my library assemblies which are downloaded was 0 bytes in size.
This meant the reference in visual studio was set, but the assembly was actually just an empty file.

Make MSBuild use new Roslyn compiler in VS15 Preview

I just recently delved into Roslyn compiler stuff and I'm wondering about some possibilities. I'm trying to build Roslyn compiler from Roslyn repository using features/tuples branch and replace the needed DLLs in VS15Preview\MSBuild\15.0\Bin folder. But when I replace DLLs and build console app project I receive the following error message
Severity Code Description Project File Line Suppression State
Error The specified task executable "csc.exe" could not be run. Could not load file or assembly 'Microsoft.CodeAnalysis.CSharp, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
Files which I try to replace: Microsoft.CodeAnalysis.dll, Microsoft.CodeAnalysis.Csharp.dll and csc.exe.
Is it possible to build your custom Roslyn compiler for C# and replace it within VS15 Preview?
One way of going about it is to set the CscToolPath msbuild variable to your new roslyn build's bin directory.
I find the msbuild commandline (e.g. Start > Developer Command Prompt) the most convenient for this. You can just cd into any solution directory on run:
msbuild /p:CscToolPath=<PathToYourRoslynBinDir>`
You can also do it inside a .csproj if you do it more often:
<PropertyGroup>
<CscToolPath>C:\your\roslyn\bin</CscToolPath>
</PropertyGroup>
Obviously, this only affects the build, not Visual Studio or it's intellisense. To hook that up, you need to follow the Trying Your Changes in Visual Studio guide in the Roslyn repo. You have to build one of the VSIX projects in the Roslyn solution. Running one of those projects launches an experimental instance of Visual Studio where you can play around with your changes.
M0sa's answer might work, however, that will only give you MSBuild enabled builds. If you also want to test it in VS, then you can follow Roslyn's own documentation. That page tell you how to replace the "intellisense" in Visual Studio (the VisualStudioSetup bullet), and the Ctrl-Shift-B "Build Solution" to actually run your solution (the CompilerExtension).
If you just want to try out your build of Roslyn, you can do that by opening Roslyn.sln in VS15 and then running the Roslyn project (with or without debugging). It will start a new instance of VS15, which uses your build of Roslyn.

"nunit.framework.dll" could not be found

I am following the NUnit tutorial here.
My source files live in the folder C:\Users\Me\Documents\Visual Studio 2015\Projects\NUnitTest\NUnitTest. My NUnit DLL lives in the folder C:\Program Files (x86)\NUnit.org\framework\3.2.0.0\portable\nunit.framework.dll. I am certain that these paths are correct.
To compile the source file AccountTest.cs into a DLL, I ran the following commands:
cd C:\Users\Me\Documents\Visual Studio 2015\Projects\NUnitTest\NUnitTest
C:\Users\Me\Documents\Visual Studio 2015\Projects\NUnitTest\NUnitTest>csc /target:library /out:AccountTest.DLL /r:C:\Program Files (x86)\NUnit.org\framework\3.2.0.0\portable\nunit.framework.dll AccountTest.cs
However, I see these error messages:
error CS2001: Source file 'C:\Users\Me\Documents\Visual Studio 2015\Projects\N
UnitTest\NUnitTest\Files' could not be found.
error CS2001: Source file 'C:\Users\Me\Documents\Visual Studio 2015\Projects\N
UnitTest\NUnitTest\(x86)\NUnit.org\framework\3.2.0.0\portable\nunit.framework.dl
l' could not be found.
Any advice?
EDIT: I didn't forget to add a reference to NUnit inside of my solution. I also included the appropriate using statement.
It is because you did not quote the path to the NUnit assembly when you compiled from the command line. It should be this,
csc /target:library /out:AccountTest.DLL /r:"C:\Program Files (x86)\NUnit.org\framework\3.2.0.0\portable\nunit.framework.dll" AccountTest.cs
You should also know that the NUnit Console cannot run tests using the portable version of the framework at the moment. To do that, you need to create a self-executing test assembly using NUnitLite. For now, it would be easier for you to just use the .NET 4.5 version of the framework.
Is there any reason you are compiling at the command line? Visual Studio Community Edition is free and will handle compiling for you. If you are not on Windows, MonoDevelop is another good option.
Have you added a reference to your Solution?
right click Solution
hover over add
click reference
search for Nunit reference and add it
Also, make sure that you have added a using statement at the top of your project on all classes

Strange dll error message

For about 2 weeks now, I have been unable to run any UnitTests (built in VS unit tests) for a project. Previously everything worked fine. The error message is:
Could not load file or assembly 'C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\PrivateAssemblies\MyProjectName.XmlSerializers.dll" or one of its dependencies.
The project references System.Xml.Serialization and uses the XmlSerializer class; just like many other classes/projects I've written. For some reason, only this project is affected. It builds fine, runs fine, I just can't run my unit tests.
I've checked the directory, and all the dlls in that directory are Microsoft dlls. The dll that it is looking for obviously is not a Microsoft dll.
Anyone have any ideas?
Edit:
It apparently has something to do with using the XmlSerializer and it generating that file automatically instead of using sgen.exe. Here is a link to the MSDN article. From what I've been able to find, it has something to do with using the serializer with generics. None of the sources I've found seem to offer any way to make it actually work.
First enable loader logging (using FUSLOGVW.exe from the SDK) to confirm what is not being found.
Then use Reflector on all your assemblies to find the one that is trying to load the non-existent assembly. If you find no such assembly it must be being loaded dynamically, in which case attaching to AppDomain.AssemblyResolve should allow you to identify where.
try to copy all your source files somewhere, then delete the project and try to make it from scratch. Maybe something happened with project dependencies
Is your computer 64bit? I got the same error when trying to run a 64bit dll with NUnit that was set to work as an x86 assembly (using corflags).
You can probably find out from the error message (use FUSLOGVW.exe lick Richard suggested).
If that is the case you can either sent the dll or NUnit to run as the correct assembly using corflags.
Solution
As it turns out, the problem was with VMWare. I installed the latest version of VMWare, and it installed it's tools to debug in a VM. Something it installed or changed caused this problem. When I uninstalled VMWare, the problem went away. So, I reinstalled VMWare without installing it's debugging capabilities and the problem did not come back.
Workaround:
I still have no idea why this problem suddenly started occurring, but I found a hack to make it work.
I had to go to project properties => Build Events and add this line to the Post-build event command line:
"C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\sgen.exe" "$(TargetPath)" /force
This forces VS to generate the file. I then had to copy that file manually to the directory it was looking for it in:
"C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\PrivateAssemblies"
Now it I can run my tests and step through them. The problems I have now are 1) I have to remember to copy the dll to that directory every time I change something in the classes that I am serializing, and 2) I now get a ThreadInterruptedException when a test finishes running; thus 3) I can only run one test at a time.
Not a good solution, but at least I can limp through. Unfortunately, redoing everything, as Nikita Borodulin suggested, is not an option.

Categories

Resources