Building C# lib - c#

I'm trying to build this code with control + alt + f5:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Formater
{
public class Startup
{
public async Task <object> Invoke (object input)
{
return ".NET";
}
}
}
But i'm receiving this error:
"A Project with an output type of Class Library cannot be started directly.
In order to debug this project, add an executable project to this solution which references the library project. Set the executable project as the startup project."
What could be the problem? I was searching SOF question and i found several solutions, but none of them is working.
Thanks.

What could be the problem?
As you say:
A Project with an output type of Class Library cannot be started directly
It goes on - if you bother reading:
In order to debug this project, add an executable project to this solution
which references the library project. Set the executable project as
the startup project.
This is all clear english. A class library (dll) has no entry point and can not be started. It is a library to be used in a program.

Since you are building a library (.dll) Visual Studio doesn't know how to run this. Visual Studio can only run programs (.exe) or websites that are loaded by a web server (among other things).
The solution offered by Visual Studio basically means that you must reference your library in some other other program to be able to run it.

You are building a class library which has not any entry point or main method. You can not run class library alone, you can only build it, for running this class library you either have to add it will a console app or win form or any of the .net application

Related

Use external, non native, dll in VS C# class application

I tried to follow some examples but I must be missing something here...
I am creating a class application .NET Framework 4.
I downloaded a required dll from the internet which I need to reference in my class application.
Should I add it or reference it in my project and how exactly should I do that?
I intend to share the finished dll with others, therefore the external dll I downloaded should be part of my final release - how to I do that?
And how to I use this dll in my class?
Thanks
Case 1:
If you have downloaded the dlls from NuGet using your Visual Studio plugin then you do not need to worry about anything.
As it will automatically update the package.config in your project
It will add the reference for this dlls to your project
Now, at the time of code check-in you should not check-in the dlls files but the package.config file only.
Case 2:
If you have downloaded the dlls manually from other sources but not using Visual studio plugin then you will have to manually add the dll reference to your project.
And you should also share these dlls with the code repository. So, that other people do not need to search for it on some other sites.
Case 3:
You can upload these dlls to Nuget.org (if security is not a concern) and make it available. in that case this will be one time process and you can just update the package.config as in case 1:
If you want to import a dll to your project, you can refer to the following steps.
1.Click Project => Add Reference...
2.Click Browser => Add the dll your want => Click OK
3.Add the using statement and call the method that in this dll.
using System;
using testdll;
namespace import_dll
{
class Program
{
static void Main(string[] args)
{
// Class1 is the class in testdll.dll
Class1 class1 = new Class1();
// Text is the method defined in Class1
class1.Text();
Console.ReadKey();
}
}
}

Unity - UWP: How to import dll/nuget-package/references to my project

Context
I got this uwp demo project which uses this sdk. The sdk is available for Windows UWP C#. Running its solution works fine and it does what it's supposed to do. Now I want to use it in my unity project, which is set up for the ar glasses HoloLens (choosed uwp as bulding platform in unity).------------------------------------------------------------------------------------------------------------------------------------ Question
How can I use the dll from the demo project in my unity project?
------------------------------------------------------------------------------------------------------------------------------------
What I tried
1. I took the dlls out of the build dir of the demo project and imported them to unity. I created a folder Assets\Plugins and put it there. Trying to use it via using Kinemic.Gesture gives me the error error CS0246: The type or namespace name 'Kinemic' could not be found (are you missing a using directive or an assembly reference?) I also tried to add the reference by using the reference manager and taking the dll from the pluing folder, but then I got this notification:
2. I took the nuget-package and changed the format to .zip, so I can unzip it and take the dlls out of it. But I got only targets:
3. I opened my unity project in visual studio by opening a script in unity. Created by right click on solution → Add new project → Class Library (Universal Windows) a new project. That way I could add the package via nuget manager:
I thought building this project should give me the dlls/references but all I get is this error in Unity:
------------------------------------------------------------------------------------------------------------------------------------
My setup
Unity 2019.2.9f1
Visual Studio Pro 2019
The only correct way of adding external DLLs to unity in the listed trials above is the first one, every other method will not compile/build or crash at runtime (guaranteed).
So how to use a UWP library in unity.
For a C# library
If it says Type: 'Managed' and Targets some .NET x.y, then it is.
1-1: In previous screenshot, if the library 'Targets .NET 4.x then edit your project settings to target .NET 4.x
1-2: Again, in the first screenshot, select the library, and edit the 'Include Platforms' with only UWP (WSA Player) platform selected, any other platform won't work including the editor.
1-3: Build your project, without trying to use the library:
Did it build safely ? great, proceed to next step.
Did it produce errors ? figure out what the reason was and why.
1-4: Since this is a UWP library, and there is no unity editor version for it, you won't get intellesense support for it, and so using Kinemic.Gesture will produce errors in the editor, any code in that library needs to be wrapped inside #if directive for UWP platform.
An #if directive simply tells unity to ignore that code until the app is running at build on specific platform, since only then will the library be usable by unity, to learn more: Platform dependent compilation
For example:
#if UNITY_WSA
using Kinemic.Gesture;
#endif
using UnityEngine
public class SomeClass : MonoBehaviour
{
public void SomeFunction
{
#if UNITY_WSA
// call some code in the Kinemic.Gesture library.
#endif
}
}
For a Native Library
It is a little bit trickier than that, but you get all intellesense support, it would still be only functional in the build tho.
One way is to interop with the library from your unity scripts using PInvoke, for more information see Unity Nativ Plug-ins
Another way, which I like is to write a wrapper C# library for this C++ library and import the C# wrapper to unity, this is achieved by:
Create a Class Library (.NET Framework) C# project.
Import Kinemic from nuget or by adding reference.
Start with a class called UnitKinemicWrapper, that looks like:
using Kinemic.Gesture;
public class UnityKinemicWrapper
{
public static void KinemicConnect(string band)
{
Kinemic.Gesture.Engine.Connect(band);
}
}
Build your C# wrapper library, and import it to unity.
Configure it with the steps at the very beginning of this guide for configuring C# libraries, in addition to that, tell unity to not process this library by un-ticking these checkboxes, then hit apply.
Now in your unity scripts you can call
string band = "band";
UnityKinemicWrapper.KinemicConnect(band);
Another approach for UWP projects only
Don't import any Kinemic libraries to unity.
Build your unity project.
Open the UWP produced project.
From there, contact unity using UnityEngine.GameObject.SendMessage, more about SendMessage

How can I include SWIG-wrapped C++ in Unity 3D?

My goal is to get a toy C++ library wrapped using SWIG, and accessible to C# / Mono scripts in Unity. (In other words, have the library functionality working in a Windows build of the game. I'll tackle Android next :)
I have been following a combination of Build a C# module (SWIG tutorial), Unity and DLLs (Eric Eastwood) and Getting started with SWiG for Visual Studio projects (Technical Recipes). I have generated two DLLs in Visual Studio 2013 and added them to the Unity project. But accessing the toy method at runtime is failing.
Steps I followed (including common fixes for the error I am receiving):
Create C++ project / custom build
Create a C++ Win32 Console Application project in Visual Studio
(because MonoDevelop on Windows cannot compile C++)
Add example code to the project
Create example.i (interface file for SWIG)
Create an Custom Build Tool for the interface file
Execute the Custom Build Tool (generates wrapper code for C++ and C#)
Add C++ wrapper code to project (will be used later to generate C++ DLL)
Create C# project
Create a C# Class Library project in Visual Studio
Change Target Framework version 3.5
Add C# wrapper code files to project root
Add the following library references and namespace to the C# wrapper files:
using System;
using System.Runtime.InteropServices;
namespace CSharpExampleLib {
...
}
Build two Release DLLs
Set the build settings to Release / x86 for both projects
Set the target for the C# build to the target of the C++ build
Build the solution (generates two DLLs, one per project)
Confirm with Dependency Walker that the DLLs do not see each other as missing (reference)
Used CorFlags to force the C# DLL to be 32-bit (reference) (this does not work / does not apply to C++ DLL)
Add DLLs to Unity project
Create a new project in Unity 4 (Pro) with a simple Mono script
Close Unity project
Copy the DLLs into Assets/Plugins folder
Reopen the Unity project (DLLs recognised)
Right-click on Assets in the Project Hierarchy, select "Synch with MonoDevelop..." (opens simple script in MonoDevelop, ensures DLLs are accessible)
Added library references to the simple script:
using System.Runtime.InteropServices;
using System.IO;
Invoke method from DLL
Finally, I added a logging call from the simple script to the C# DLL (Intellisense confirms that the DLL and method are accessible):
Debug.Log(CSharpExampleLib.example.fact(3));
// should log the result of 3 factorial
Runtime error
However, when I start the game from the Unity editor, I get the following error:
DllNotFoundException: example
CSharpExampleLib.examplePINVOKE+SWIGExceptionHelper..cctor ()
Rethrow as TypeInitializationException: An exception was thrown by the type initializer for SWIGExceptionHelper
CSharpExampleLib.examplePINVOKE..cctor ()
Rethrow as TypeInitializationException: An exception was thrown by the type initializer for CSharpExampleLib.examplePINVOKE
CSharpExampleLib.example.fact (Int32 n)
SimpleController.Start () (at Assets/scripts/SimpleController.cs:10)
I made several attempts to correct common causes for this error (build as Release, build as 32-bit, check Dependency Walker, etc) to no avail. Is there something SWIG- or Unity-specific I am missing? Any other checks I can perform on my DLLs?
After discussion on the SWIG user mailing list and some careful review, I discovered I was missing two configuration steps:
The C++ DLL must have the same name as the module name specified in the SWIG interface file (so for my toy example using interface file example.i, it is best to name the C++ project "example" and it will generate example.dll)
In the backwards and forwards of resolving other issues, I lost the reference to the autogenerated C++ wrapper (example_wrap.cxx) and needed to re-add it to the C++ project.

I can't see a referenced class of another namespace in C#

I have 2 projects:
ConstrainedScheduleInterfaces
ConstrainedSchedule that has a folder (Testing) with my class that need the reference, here's the code:
Tests.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using
using NUnit.Framework;
using Ninject;
using ConstrainedScheduleInterfaces;
namespace ConstrainedSchedule.Testing
{
internal static class Configuration
{
...........
}
}
I added the reference to the ConstrainedSchedule project, but the using ConstrainedScheduleInterfaces; is marked red as not found.
Both the project has destination framework set .NET Framework 4.5
Any help? Thanks
Does the project contain a reference to the other project? You can't just add the namespace, the project itself needs an assembly reference to the other project which has that namespace.
In Visual Studio, open the Solution Explorer. Right-click on the ConstrainedSchedule project and select something along the lines of "Add Reference." In the dialog, select project references (depending on the version of Visual Studio it may be a tab or some other interface indicating projects as part of the solution). Select the ConstrainedScheduleInterfaces project and add the reference.
More information here.
For other people who have this problem, who have already added the reference to the dll and have made sure you have the right namespace at the top, I've found another thing that can cause this.
Select-click the file in visual studio and choose properties. In the Advanced part of properties, look for Build Action. Change it to compile if it's not already on that, and try to build again.
There might be another reason: different target frameworks.
My main project had ".NET Framework 4.7.2" and the referenced project (linked from another solution) had ".NET Framework 4.5.1".
Select the project properties and change the target framework.
I was having this issue after renaming a project and namespace due to a naming conflict.
Basically my main project had a reference to a project I made called Common, but then I needed to reference another project called Common that another dev had written. After renaming my project something like SpecificCommon, I was able to have references to both Common and SpecificCommon in my main project. However, all references to SpecificCommon namespace were now broken.
At first I thought this was some problem with the namespace refactor, but it turns out that it was because SpecificCommon and Common both still had the same assembly name (Common.dll). Changing the assembly name in SpecificCommon's properties and rebuilding solved the issue for me.

open c# namespace from same solution, referenced project in f#

I am still trying to get my head around namespaces in referenced projects.
I have a c# project where a namespace ("A") is defined.
I create a F# application, where I reference the c# project.
open A
leads to the following:
error FS0039: The namespace or module 'A' is not defined
This is what I have checked:
The C# and F# project target the same framework (.Net Framework 4.5) (F# the namespace or module 'XXXX' is not defined)
The C# and F# project have the same target (Any CPU)
The C# project is correctly listed among the F# project references
I am correctly trying to open a namespace (not as in this case: How to use C# object from F#?)
I am not operating from the FSI (https://connect.microsoft.com/VisualStudio/feedback/details/632859/difference-in-namespace-reference-between-f-interactive-and-compiled-exe)
MSDN is not much of a help (http://msdn.microsoft.com/en-us/library/vstudio/dd393787.aspx).
Thanks.
[EDIT]
I have started from scratch, so that the example is lean and clean.
The C# project "ClassLibrary1" contains:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClassLibrary1
{
public class Class1
{
}
}
The F# Application is:
// Learn more about F# at http://fsharp.net
// See the 'F# Tutorial' project for more help.
open ClassLibrary1
[<EntryPoint>]
let main argv =
printfn "%A" argv
0 // return an integer exit code
The error:
The namespace or module 'ClassLibrary1' is not defined
Make sure your reference is a real project references and not just referencing the DLL in the bin\Debug folder.
If you can't get a real project reference to work, make sure the F# project is set to depend on the C# project so that it gets built first by Visual Studio (right-click on the project and go to Project Dependencies then use the tickboxes).
I had this same issue. All I had to do was make sure my C# project dependencies were all settled, build my C# projects, then the F# project was able to reference the correct dll's.
In Visual Studio, you can right click on each C# project and press build.
In VSCode, you have to use the terminal to navigate to each project and run dotnet build.

Categories