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.
Related
I am working on a c# project. It reference quite a few packages and there are packages referencing other packages. So a namespace used in the program does not necessarily come from a direct reference.
For a specific using statement, is there a way to find out which reference (by reference, I mean the external DLL's/NuGet packages) it is originated from?
Thank you.
For example project reference a Nuget Package called Package1. In Package1 we have namespace called Namespace1. Then Package1 references Package2, which have a namespace called Namespace2.
In your code you could have
using Namespace2;
But how do you know which assembly or Nuget Package Namespace2 is originated from (in this case Package1)?
I'm not aware of a native means in Visual Studio to do this. However, JetBrains ReSharper can do this. Note that a single namespace import isn't tied to a single assembly. A namespace import (using statement) can easily bring types from several different assemblies into scope.
With ReSharper in hand I simply selected the namespace and pressed F12. You can see that System.Collections.Generic is defined in four assemblies referenced by this project.
I tried harlam357's answer in visual studio 2019 but it failed (the image bellow).
1.
Then, I did a trick. I turned the reference in to a comment and found the error line which implements a method belong the namespace. Through the method I got the root reference.
2.
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
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 have a solution in Xamarin Studio with a C# console program trying to call an F# library.
The reference is fine, everything builds.
My F# code looks like the following:
namespace MyStore.Library
module public Lookup =
open System
let lookup name (age:int) =
String.Format("{0}, {1}", name, age.ToString() );
I can call the F# library from an F# console and a C# library from the C# console, but I can't mix the two.
The C# console doesn't see any F# namespaces at all.
What am I doing wrong?
What should I do to see the F# namespace from C#?
In the C# project, you need to add a reference to your F# project.
In the Solution pane, expand your project, and click the gear icon -> Edit References
Then go to Projects tab, check the F# project you want to reference to, then click OK.
Also, make sure both C# and F# project target to the same .NET framework! To check that, right click a project, Options -> Build -> General
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.