How to separate Class Library into multiple assembly (*.dll)? - c#

I build a class library project with separate class by folder.
Example:
TEST
Security
Converter
Utility
After I compile project, I will have TEST.dll
but I want TEST.dll, TEST.Security.dll, TEST.Converter.dll, & TEST.Utility.dll
Note: Security, Converter, Utility is folder in TEST project. In each folder have c# class.
How to do?

Create separate projects for each
Example:
name project TEST.Security class library
name project TEST.Converter class library
name project TEST.Utility class library

the fact that you separate classes by folders means nothing when it comes to the dll output.
If you want one dll per folder then you will have to create one separate class library project for each one. In the properties of each project you can set the name of the dll file itself.
This being said, the name of the dll is less important, it's the namespaces that will matter. The namespace is set in each class file and this will affect your "using" statements.

Related

using the class in the project group in all projects

I have small projects that are alike, using the same class,
I keep the same file in every project, But when I want to make a change, I have to visit all my projects and update them.
While developing a project in Delphi, I would put my related class in just one directory , I would just update in that class and build all of them, all projects would be updated at the same time
how do i do this in visual studio , I made a project group, I added a class to the group, but the projects in the group cannot access this class. How should I use a method?
common class I will use in all projects , 2nd and 3rd other projects, there is more
You should start with a class library (dll) project with your shared code, then add reference to that project/library on the projects you'll need to consume it.
Here's a Microsoft tutorial about this:
https://learn.microsoft.com/en-us/dotnet/core/tutorials/library-with-visual-studio
You can create new project of dll library. This library can be referenced by all other projects. This is way how you can use the same interface in all other projects.

Visual Studio C# Namespaces And Assemblies

I have a problem understanding the difference between namespaces and assemblies.
So let's say that I make open Visual Studio, and I create a new project. I will name the project "Project A". The Solution Explorer will look like this:
Now, as far as I understood, the "Solution 'Project_A'(1 project)" is the assembly & "Project_A" that is right under it is the first namespace. Now, I know that I can add multiple "nested" namespaces with different classes. So I can make another class called X and then make a new folder in "Project_A", so a new namespace that will be called "MainClasses" and add the classes A & B there so that it would look like this:
So now, if I'm not wrong: I have the assembly "Project_A" that has the namespace "Project_A". The namespace "Project_A" includes a class called X & another namespace with classes A & B.
Now, if I go to "Solution 'Project_A'(1 project)" and I click on Add->New Project, I will make a new namespace with the name "Project_B", and add another class to the new namespace called Y, I will now have:
The assembly "Project_A" that will contain the namespace "Project_A" & "Project_B", and it will look like this:
Can somebody please correct me if I am wrong and tell me the right way. So what is the exact difference between namespaces & assemblies when working with c# in visual studio. Showing some screenshots would be the best, if you can do it, of course. Thank you
An assembly is an exe (executable) or a dll (dynamic link library) and it is a software primary "component" (not in the sense of OOP component or control). Sometimes named package.
What exactly is an Assembly in C# or .NET?
https://learn.microsoft.com/dotnet/standard/assembly/
A namespace is a code organization feature.
https://www.tutorialspoint.com/csharp/csharp_namespaces.htm
https://learn.microsoft.com/dotnet/csharp/programming-guide/namespaces/
An assembly that is like a partition can contains one or more namespaces that are like folders.
When an assembly is added to the references of the project, you can access to all its namespaces with the using directive at the beginning of the file or by specifying full access directly in the code.
Example
The assembly System.dll contains several namespaces like System and System.IO as well as System.Threading and so on.
using System.IO;
var lines = File.ReadAllLines(...);
Or:
var lines = System.IO.File.ReadAllLines(...);
      ...
These two concepts are not related. It's only by default that your initial namespace takes the name of your project.
By default each of your projects, contain the global namespace and your own. You can rename the default name of your namespace to anything you want without an issue.
The assembly:
Assemblies form the fundamental units of deployment, version control, reuse, activation scoping, and security permissions for .NET-based applications. An assembly is a collection of types and resources that are built to work together and form a logical unit of functionality. Assemblies take the form of executable (.exe) or dynamic link library (.dll) files, and are the building blocks of .NET applications. They provide the common language runtime with the information it needs to be aware of type implementations.
The namespace:
Namespaces have the following properties:
They organize large code projects.
They are delimited by using the . operator.
The using directive obviates the requirement to specify the name of the namespace for every class.
The global namespace is the "root" namespace: global::System will always refer to the .NET System namespace.
The namespace for the project is set in the project properties. It is by default set to the assembly name and class files inherit this name when created, but you can change to any name you like. If you add a folder and put a file in it, the folder name gets appended to the parent (for the first folder this is the assembly) namespace. Again you can change this to any arbitrary name.

Not getting path to class library project from within class library project

I am calling a helper method in my Services class library project from a controller in my UI web application. I cannot get the proper path to the templates in the services project. I have tried dozens of ways but every time the base path of the full path points to the UI project.
C:\Users\TFD\OneDrive\TestEmal.UI\TestEmal.UI\bin\Debug\netcoreapp2.0\EmailService\EmailTemplates\EmailMaster_Body.html
I am building the path in the Services class library project
private static readonly string ThisDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
This came from the accepted solution from this post, accepted answer by mark Amery
Class library path SO
I have tried every permutation of Path and of Assembly but every one returns the path to the Web UI application.
How do I get the base path of the Services class library project without hardcoding or using Replace?
You cannot. A class library is compiled into the application that references it. After build/publish, the DLL for the library resides in the same directory as all the other DLLs for your application, meaning any paths will always be relative to your web app directory, not your class library directory.
It's not entirely clear what you're trying to achieve here, but if there's simply some file or files in your class library project directory that your class library needs to reference, you need to add them to your project and set them to copy on build in the properties pane for each file in Visual Studio. This will result in the file(s) coming along for the ride and ending up in your web app's build/publish directory as well. Your paths will still be relative to the web app, not the class library.
Alternatively, you can have a build task that does the copy instead, but that's a little more complicated to set up.

Is it possible to import class file without converting it to a dll in c#?

There are a lot of variables and methods in my program and I want to seperate some of them in other class files. But as the program grows the methods and functions can change.
I searched on the net but many people generally speaking for dll files. Without making a dll file, how can I arrange my code and split into small class files?
Yes, just split it out in to a separate file in a new class but still inside the same project. The term for what you are doing is called Code Refactoring. There are some tools built in to Visual Studio to make it easier to do, and there are some 3rd party tools that add even more features to make it easier to do.
But all it boils down to is just making new classes in the same project and referencing those new classes from where you took the code out from.
You can add folders to your solution. Classes are by default a namespaceprovider, so that classes in this folder have a different namespace.
For example if your default-namespace is MyNameSpace and you create a folder called Entity then all classes in this folder have the namespace MyNameSpace.Entity
And all Items in a project are compiled to one single dll or exe
Just add more classes to the project and put the data and behavior (methods) into the appropriate classes. The project will still build into a single exe or dll.
Generally, it's better to add a second project under the same solution call it "CommonLib" or something like that. Then you add it as a reference to the main application and set up the project so that the applications build depends on the libraries build. Add a using statement for the common lib where ever you want to use those objects. This is definitely better for large scale or enterprise applications. There's a pretty decent chance that somewhere down the line you'll want to reuse some of this code, if everything builds into a single exe that won't be an option.

Program does not contain a static ‘Main’ method suitable for an entry point

What do I do if I just want to create a project which contains a bunch of library functions? In other words no Main method is required. It seemed to be compiling a minute ago and then I added another .cs file and now I am confronted with this error message.
Create a .NET Class Library project if you only want a library project. If this is a project that already exists, you can set the Project Output type to a DLL ("Class Library") instead of an Executable ("Windows Application"/"Console Application") in the project properties.
What type of project did you create? It sounds like you meant to create a class library but accidentally created an executable assembly. Ensure that you are in fact creating a class library assembly (i.e. ".dll" not ".exe").
If you aren't using Visual Studio and are compiling your code with csc.exe then make sure that you are specifying /target:library to compile your code into a library.
You want to make the project a Class Library type. I believe you can change the type of project in the project properties settings.
or you could use the tried-and-true empty main method
I have the solution. Really simple. You wrote the static void main with lower case. You should write it like this: static void Main()
This problem is occured when we deleted App.xaml file from our project after the required method written due to that please ensure that your App.xaml file is in correct format with respective namespace and references, if it is not, create it and add it in your project.

Categories