How do I get an empty project to compile as a dll? - c#

I created a new VS "empty" project, and in it made a class. I decided to try this out as a library, went into properties and set the output type to class library. It compiles to a dll, though when I add it as a reference to another project, typing in "using ..." doesn't have my new library in there. If I create a new library project, past my class in there, compile that into a dll, then it works fine. So what I want to know is, what settings to I need to change in a blank project to get it to act as a dll?

using directives are about namespaces, not assemblies.
If your library is empty, it's not contributing anything to a namespace, so a using directive won't find anything.
It's very important that you understand the difference between a namespace and an assembly - you could have library Foo.dll which only contains Bar.Xyz. You would add a reference in your project to Foo.dll, but a using directive for Bar.
As a more concrete example, the Enumerable class in the System.Linq namespace comes from System.Core.dll - but you still add the using directive for System.Linq, not System.Core. Indeed, if you try to add a using directive for System.Core, you'll get an error - because that namespace doesn't exist. (A namespace effectively doesn't exist if it has no members.)

Related

Can't add popular namespaces references to C# .DLL Class Library

My question in simple few lines:
I'm trying to build a compiled .dll C# Class Library that contains functions I want to easily call from other non-C# applications.
My functions depend on popular namespaces' Assemblies references like System.Windows.Forms and Microsoft.Office.Interop.OneNote.
When I try to add any of them to my class library, I get does not exist in the namespace error.
Also, they don't exist in Reference Manager (Screenshot 2).
Continued description:
Within normal C# application (that's compiled to .exe), I can easily include those Assemblies references using code lines like below:
using System.Windows.Forms; // works good in normal application
using Microsoft.Office.Interop.OneNote;
Also I am able to add References with References Manager (by right clicking the References node on Solution Explorer → then Select Add Reference → then search for the references I want to add) — in Visual Studio 2019 (Screenshot 1). I can find and add assemblies like System.Windows.Forms and Microsoft.Office.Interop.OneNote easily.
But within a C# Class Library (that's compiled to .dll), when I try to include System.Windows.Forms and Microsoft.Office.Interop.OneNote using the below lines:
using System.Windows.Forms; // gives errors in C# Class Library
using Microsoft.Office.Interop.OneNote;
I get errors:
CS0234 The type or namespace name 'Forms' does not exist in the namespace 'System.Windows' (are you missing an assembly reference?) MyLibrary C:\Path\to\MyLibrary\Class1.cs
CS0234The type or namespace name 'Office' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?) OneNoteLibrary C:\Path\to\MyLibrary\Class1.cs 15 Active
Also, these assemblies aren't shown at all when I try to add using References Manager (Screenshot 2).
Screenshot 1:
Screenshot 2:
Screenshot 3:

VisualStudio.Net reference not working

I create a new solution called "WordpressAutomation"
I create a new project, a class library, called "WordpressAutomation" in this solution
I create a new project, a unit test project, called WordpressTests in this solution
I add a reference to the project WordpressAutomation in the project WordpressTests"
This should be ok, shouldn't it?
However, as can be seen from the screenshot, when adding this using line in a class in the project WordpressTests,
the reference doesn't work. Even though it is there, as can plainly be seen.
When entering "using", the class library "WordpressAutomation" doesn't appear among the alternatives.
And I get this error message when trying to run:
"The type or namespace "WordpressAutomation" could not be found (are you missing a using directive or an assembly reference?)"
Why is this happening? The reference is clearly there?
http://i.stack.imgur.com/L0g5R.png
Your project isn't actually including the .cs files to compile; they are only shown for information (they should appear as solid files with the C# logo):
Right click on these files and include them in the project. Then check what namespace they declare inside them. At the moment, your WordpressAutomation project simply doesn't contain any types.

Library reference imports ok, intellisense sees everything, objects instatiated... then I build and it can't find anything?

I run into this every once in a while and I never really figured out how to fix it. I have a class library that is in the same solution as the test console project. when I add a reference to the library in the console project and add the using statement, intellisense sees all of the classes just fine. So I write some code and use those classes. All of the class members are showing just fine. Then I hit build. I get an error that the classes from the library cannot be found (are you missing a using directive or an assembly reference?). Now all of a sudden, all my objects are underlined in red and no longer color coded as classes, and the using statement is also underlined in red. The reference to the library is still in the references folder. Wtf
If I remove the reference and readd it, its the same thing. It'll see everything just fine until i build.
Check that warning in your screen, saying "Referenced assembly UTILibrary could not be resolved because it has dependency on System.Web....". UTILibrary is probably using System.Web internally, so this dependency must also be added to TestConsole project.
Add it and all should be fine.

How to diagnose an issue with csproj references?

I have an issue with a class being used in another project.
Visual studio is able to add a using statement using Resolve (Ctrl+.), but:
The type or namespace name 'SomeClass' could not be found (are you missing a using directive or an assembly reference?
Resolving this again fully qualifies the type but the namespace in the type path is highlighted as not being found.
The namespace appears also in intellisense and the target class is visible in the Object Browser.
Removing unused namespaces removes the added using statement.
I've tried:
Rebuild.
Confirm project dependency in solution options.
Delete reference and re-add.
Confirm class accessibility (despite namespace not being found).
Checked build mode (Debug/Release).
Restart Visual Studio.
Cleared bin folders in both projects.
Restart machine.
What is the next step to diagnosing this baffling issue?
This is likely to be a .Net framework versioning issue. The class being referenced was probably built using a higher version of the framework.
Try checking your framework versions all align in properties, build settings. A project cannot reference an assembly of a higher framework version.
Last time I had this problem my class was not marked public. Just an idea.
This issue was caused by similar namespaces e.g. Company.Product.Project and Product.Project with:
namespace Company.Product.Project
{
using Product.Project;
The class was in Product.Project, but the compiler was searching for Company.Product.Project.
See: Should 'using' statements be inside or outside the namespace? for more details.

Referenced class lib doesn't seem

I have a class lib and I referenced it to another windows project. I added its namespace to my Form.cs (using SaglikNetClassLib;). they are seem unknown when I want to access to classes. I can see their properties and methods. But the complier says "Error 7 The type or namespace name 'SaglikNetClassLib' could not be found (are you missing a using directive or an assembly reference?), Do you have any suggestion?
KR,
Çağın
Are you targeting the .net Client Framework? Visual Studio let's you add references to incompatible assemblies, but then gives exactly that error.
Check your project settings to make sure you're targeting the full .net framework.
Also, check that the Ilalcar class is public and not internal (which is the default if it's only declared as class without any modifier)
You probably need an using statement to put the class into scope.
using SaglikNetClassLib;
C# won't auto suggest it if the project has not been rebuild. Also make sure the class library project has been build before using it in code.
Intellisense seems to lag behind a bit at times. Simply pressing f5 (run) sometimes rebuilds the project completely and simply runs or gives a better error message.

Categories

Resources