Imagine I have different methods written in c#. And these are included in the main class now and work completely fine. But I have a plan to add an upadter function to this project. In this case my idea is to include each function in a DLL file with the function name. So my updater function can easily replace the old DLL file with the new if there's a new version available. The problem is I don't know how to create a DLL file by just including a method.
You shouldn't make a seperate .dll for each function. When writing the library I suggest adding a new project to the current solution of the type C# class library. Now you can easily use your newly written library inside the project you are using it for. Like this:
in the solution explorer you see a library project and a windows forms application project. The windows forms application uses the library's code to work. You can use the library when it's finished and compiled to a .dll by referencing it in the other project(s) under 'add reference'
Related
I'm not new to C# programming, but I suppose I'm new to programing "the right way" in C#. I've worked in C on embedded devices for years and have written desktop apps to support them. First in VB6, then in C#.
I recently started making better use of classes for reusing code (and for instantiating more than one instance of the class in a program). For example, I "wrapped" a UART interface with some additional functionality so I can use the same code for multiple ports by creating an instance of the class for each one.
It is in a separate file, but still in the same program namespace, so when I want to reuse it, I have to copy the file and change the namespace to the new project.
I'm sure there's a way to create it such that I can just reference it like everything else with either a "using..." reference at the top of the program or with a "Project | References..." checkbox. But for the life of me I can't find a good learning journey for this.
Any direction would help.
You want to create your reuseable class in an assembly - this is the equivalent of a dll from your C experience.
To create an assembly, have a separate project of type assembly (instead of exe) . You can reference the assembly from other projects. If your project is in the same solution you can reference the project, otherwise you can reference the compiled assembly.
C# uses a packaging system called Nuget, so you can package your assemblies into "Nugets" which you host in a Nuget Server. You can then use tooling to discover and import these.
Please create a Class Library project and include your class into that project. Make sure your class is public. Once you build this project you'll get an assembly which can be referenced from other projects. See Tutorial: Create a .NET class library using Visual Studio
There are different ways of referencing it.
You can have the class library project in the same solution as the main project. In this case you should add a project reference.
You can copy the compiled *.dll file to some folder in your solution (e.g. Lib) and add an assembly reference.
If this assembly is to be used in multiple projects please consider creating a NuGet package with this library and pushing it to some repository. Then other projects can add a package reference to this package.
Details:
How to: Add or remove references by using the Reference Manager
Install and manage packages in Visual Studio using the NuGet Package Manager
It is in a separate file, but still in the same program namespace, so when I want to reuse it, I have to copy the file and change the namespace to the new project.
Well, it isn't the best practice but (unfortunatly) still a common behavior. So don't worry to much about it.
What you could do to improve it place the file (and other reusable parts) in a seperated csproj.
For example name the project of the type class library and name it VinDag.Tools. Within the project create a folder UART and place the wrapper there. The namespace of the wrapper would then be VinDag.Tools.UART.
From know on you can just reference the class library instead of renaming the file. It's not necessarily required to be the same namespace as the project.
From there you can start considering (private) nugets. This would prevent you from copying files/csproj around.
please, I have a question about snap7.dll library. So long time I wanst be working in C#, so maybe I am doing something wrong. But is possible to use snap7 in C# WPF project as library or it was developed only for windows forms? Its stupid I know, but I am asking because I am not able to add snap7.dll into my project references. Thank you.
Downloaded and played around with the examples from http://snap7.sourceforge.net/
Looks like the console application doesn't reference the assembly directly.
This isn't a WPF vs Winforms thing. It is a managed vs unmanaged code thing.
There is a snap7.net.cs .net wrapper class file.
It references "snap7.dll" and exposes its functionality as a C# class. At runtime it will load the assembly using DllImport.
Copy both the snap7.net.cs file and the snap7.dll into your project.
Use the snap7 class methods/attributes in your code. Then update snap7.dll to copy to output directory, or use a post build event to copy the snap7.dll to your output directory.
EDIT: I want to restate you do NOT add reference to the snap7.dll directly using project -> references. The DllImport annotation of the wrapper class file will load it at runtime.
I have made one common libarary using c# (.dll file is created) that is having some common functions .
Now I have one solution which is having so many projects made on vb and c++ .
Now here I added that above dll file in this solution (by adding refrence).
and I want to access those common function from dll to this all projects .
Is this possible??
IF YES THEN HOW??
For each C++ and VB.NET project, right-click on References and add the C# project as a reference.
Then access your c# classes & methods as you would access thus from the .NET "built-in" libraries.
If it's not a project within your solution, then for each project, browse to the dll and add it that way.
I have one windows service project. I want to convert that web service project into DLL. As a class library I can simply reuse those methods just by referencing that DLL.
I just want to know is there any simple way to do it or I have to manually create one class library project and reuse my code.
You can reference exe in other projects same as dll.
But moving this to separate class library is the best idea.
Just Click on the class library with the service code and select build. it will create a DLL file in your Debug folder in your project bin directory. You can use that DLL file anywhere.Hope this helps..
Just copy your code to class library project and built it. It will create .dll and you can easly use it by adding reference to your project. I think that would be a simplest approach
After searching and asking questions on ASP forums proper way of doing this is to manually reuse code.
When creating DLL files for a program that already exists, is it customary to create them by going to
File >
New >
Project >
Class Library,
File >
Add >
New Project >
Class Library,
or
File >
Add >
Existing Project >
Class Library?
You should differentiate 2 things:
DLLs - compiled code in machine (in case of .net, clr) readable format for execution
Code - source code files that get compiled into DLLs.
If you have a dll that you can use, you add it as a reference to your project.
If you have source codes, you can add them the way you specified (add existing project). If you want to write new .dll, you should use "Create new class library". Note that whenever you add through "Add existing project", project (along with source codes) isn't copied to your solution folder.
Basics you should know before you go on:
Solution - means to tie several projects together into one logical bunch.
Project - means to tie several source code files/resources/etc. into one logical bunch that gets compiled into one physical unit - dll/exe/etc.
Source file - code file like MyClass.cs. This is where the code is written.
Reference - reference from one project to another one in order to obtain/use public-visible functionality.
I'd recommend reading some books on C#/.NET to get clearer understanding.
You are right, that is one way of creating dll,(for a class library in visual studio, you will get a dll) but its lot more than that.
You should gather some knowledge for DLL check out this link
http://msdn.microsoft.com/en-us/library/1ez7dh12.aspx
Also check out this link how to create dll in c#
http://msdn.microsoft.com/en-us/library/3707x96z(v=vs.80).aspx
Well what you have mentioned in your question is a good and simple way of creating DLL with C# (that is with Class Library project) , further more you can direct your Compiler (csc.exe , it the Csharp compiler) with some commands to make a DLL for you,
Consider that you have few classes such as Add.cs (can Add numbers) Mult.cs (can multiply)
To build the file MathLibrary.DLL, you can use command like this
csc /target:library /out:MathLibrary.DLL Add.cs Mult.cs
The /target:library compiler option tells the compiler to output a DLL instead of an EXE file.
The /out compiler option followed by a file name is used to specify the DLL file name.
P.S: Solution derived from How to: Create and Use C# DLLs
Referencing Custom Made DLL in C# Projects:
Add a reference of the DLL
Add namespace in you project (or just start using DLL by fully qualified name)
Snapshots