Is assembly in C# similar to the translation unit in C++? [duplicate] - c#

Could you please explain what is an Assembly in C# or .NET?
Where does it begin and where does it end?
What important information should I know about Assemblies?

An assembly is the compiled output of your code, typically a DLL, but your EXE is also an assembly. It's the smallest unit of deployment for any .NET project.
The assembly typically contains .NET code in MSIL (Microsoft Intermediate language) that will be compiled to native code ("JITted" - compiled by the Just-In-Time compiler) the first time it is executed on a given machine. That compiled code will also be stored in the assembly and reused on subsequent calls.
The assembly can also contain resources like icons, bitmaps, string tables and so on. Furthermore, the assembly also contains metadata in the assembly manifest - information like version number, strong name, culture, referenced assemblies and so forth.
In 99% of your cases, one assembly equals a physical file on disk - the case of a multi-file assembly (one assembly, distributed across more than a single file) appears to be a rather odd-ball edge case which I've never encountered so far in my 5+ years of .NET development.
In a multifile assembly there would still be only one assembly manifest in a DLL or EXE and the MSIL code in multiple netmodule files.

The answer is in order for immediate-grasping.
Put simply, it is the compiled project involving your classes and additional files, if there are. That is, each project in a
solution is assembly.
Or more techinally,
An assembly is where a type is stored in the flesystem. Assemblies are
a mechanism for deploying code. For example, the System.Data.dll
assembly contains types for managing data. To use types in other
assemblies, they must be referenced. - Source
How do we know it? If you glance at properties of a project under the solution you can see the following images.
When you compile the project, it turns out to DLL or EXE.

.NET assembly
In the Microsoft .NET framework, an
assembly is a partially compiled code
library for use in deployment,
versioning and security.

http://www.codeguru.com/columns/csharp_learning/article.php/c5845
An assembly is a file that is automatically generated by the compiler upon successful compilation of every .NET application. It can be either a Dynamic Link Library or an executable file. It is generated only once for an application and upon each subsequent compilation the assembly gets updated.

Here's another explanation of the make up of .NET Assemblies, a mini-quote:
The .NET framework consists of the
concepts of modules, assemblies, which
both store metadata and manifest
information. An assembly can contain
multiple modules. Visual C# only ever
creates one module which is turned
into an assembly by the C# compiler
(csc.exe), but an assembly can link
many .NET modules together via the
assembly linker (al.exe) command-line
tool. For example each of your source
code .cs files could be compiled into
a module and linked together to form
an assembly - an assembly is just a
collection of modules and resources.
One of these modules, however; must
contain manifest metadata (see below)
information for the assembly to be
understood by the CLR.
....
Having created a new .exe or .dll
inside VS.NET you see your file appear
inside your bin folder. Opening it in
notepad will give out gibberish, or
even inside a hexadecimal editor
without knowing the structure of the
file, you need a tool like ildasm.exe
or CFF explorer to make meaning from
it. The structure of the assembly is
as follows:
PE header
CLR header
CLR metadata
CLR
IL code
Native data

When a source code is compiled by the language compiler it Generate a Managed Assembly and MSIL(MisroSoft Intermediate Language). That Assembly contains .dll or .exe file. An Assebmly can be of two types Private Assembly and Shared Assembly, shared Assembly is stored in GAC(Global Assembly Cache) so that any application can refer to it while private assembly is stored in application folder which can be used by only one Application.

An assembly is a DLL or an EXE which will be created when you publish it or compile your application.

After writing source code of your program(project) then a file is created which may be DLL or EXE depends on your project. It makes only once for a single project. It has two types
1:- single
2:- shared or multiprogram
single assembly used only in a single program while shared can be used for multiprogram

An Assembly is a collection of logical units. Logical units refer to the types and resources which are required to build an application and deploy them using the .Net framework. Basically, Assembly is a collection of Exe and DLLs. It is portable and executable.

Related

Could not load type 'myinterface' from assembly

I have a solution that includes several projects. A few are libs that are building dll's used in my main project in this solution.
My main project builds with output type console application.
This all works fine.
When i change the build output type to a class library (since i want to use this project as a plugin eventually). The project will still build, this time to a dll.
When i use this plugin in an application where i use it as a dll however, it will run up to a certain point where it's trying to load a type defined in an external dll (so NOT built by my solution) and throw the exception:
Could not load type 'externalinterface' from assembly 'externallib, version=3.0.0.0, Culture=neutral, PublicKeyToken=null'.
The dll's are all in the correct folder,etc.
Also worth noting, the plugin is tested in another location than where i built it. The executable still works on this location, the dll/plugin does not. Same amount of dll's in their folders etc.
EDIT: I already used ILSpy (dll inspector) to open the actual dll that is being referenced (so externallib in the errormessage) and checked if 'externalinterface' was present and it is.
EDIT2: RESOLVED! The program that loaded my plugin was loading the same dll that caused the exception. The dll it loaded was of another version than the one i loaded.
Check whether the type externalinterface is present in the referred dll.
You didn't include the details of the exception the application is throwing. However, based on the message you gave, it appears your assembly does not have a strong name. If the application attempting to load your assembly as a plugin does have a strong name, then .NET will require all assemblies loaded by it also have a strong name, so you need to configure your assembly to have a strong name before continuing.
Maybe some supported dll's which is used by the 'externalinterface' is missing in the target machine. In the target machine, check is all the necessary dll's are present in the output folder.
Or blindly copy paste all the dlls in the output folder from the machine where the code is working to the target machine where you have the problem. After this, if the code is working in the target machine, then try to analyze which supporting dll you are missed to copy.

Assembly + Meta Data + Assembly Manifest + MSIL CODE

Can any one will answer the following questions. I am using c# language.
Can I call Assembly as .ddl or .exe file?
Can I call Assembly Manifest as assembly?
What is different between Assembly, meta data and Assembly Manifest?
Can I say
.DLL or .EXE = Assembly + Meta Data + Assembly Manifest + MSIL CODE.
Please help me to clear these questions.
1 -- An assembly can be a single a .DLL or .EXE file, but not all .DLLs and .EXEs are assemblies. Assemblies are specific to the .NET framework. Both .EXE and .DLL assemblies can be referenced or loaded by your .NET application.
2 -- The assembly manifest is just one part of the assembly that contains metadata describing the assembly. From MSDN the assembly manifest:
Enumerates the files that make up the assembly.
Governs how references to the assembly's types and resources map to the files that contain their declarations and implementations.
Enumerates other assemblies on which the assembly depends.
Provides a level of indirection between consumers of the assembly and the assembly'simplementation details.
Renders the assembly self-describing.
You can add and change some aspects of the assembly manifest from your code if you need to change the metadata associated with that specific assembly. The assembly manifest may be stored as its own physical file, but it is still considered part of the assembly.
3 -- The assembly is composed of CIL code, metadata that describes the types defined by the CIL code, the assembly manifest which is metadata that describes the assembly and other resources such as static images needed by the assembly. Therefore, the assembly manifest is just one part of an assembly as is the metadata.
4 -- Almost. Assembly = MSIL Code + assembly manifest + type metadata + resources.
The assembly can be compiled into either an .EXE or .DLL file, but can also be comprised of multiple files.
Lets start with these concepts (in simple) then we will have our answers
Compilers are syntax checkers and “correct code” analyzers. in .Net platform regardless
of which compiler you use, the result is a managed module. A managed module is a standard
32-bit Microsoft Windows portable executable (PE32) file or a standard 64-bit Windows
portable executable (PE32+) file that requires the CLR to execute.
IL code Code the compiler produced as it compiled the source code. At
runtime, the CLR compiles the IL into native CPU instructions.
Metadata Every managed module contains metadata tables. There are two main
types of tables: tables that describe the types and members defined
in your source code and tables that describe the types and members
referenced by your source code.
Assembly is an abstract concept that can be difficult to grasp initially. First, an assembly is a logical grouping
of one or more modules or resource files. Second, an assembly is the smallest unit of reuse,security, and versioning. Depending on the choices you make with your compilers or tools,you can produce a single-file or a multifile assembly. In the CLR world, an assembly is what we would call a component.
(The CLR doesn’t actually work with modules, it works with assemblies)
Assembly Manifest is part of the assembly that describers set of files inside the assembly
An assembly allows you to decouple the logical and physical notions of a reusable, securable,versionable component. An assembly’s modules also include information about referenced assemblies (including their version numbers). This information makes an assembly self-describing. In other words, the CLR can determine the assembly’s immediate dependencies in order for code in the assembly to execute.
actually when using C# compiler, generatin .DLL Or .EXE will be done using a compiler option so DLL and EXE are equal components in assembly concepts.
So we will have
1- Correct, Assembly is Maneaged DDL(s) or EXE files
2- No, The assembly manifest is just part of the assembly
3- Described in the answer
4- Managed .DLL or .EXE = Assembly (Containing Meta Data + Assembly Manifest + MSIL CODE)
Images and some of the quotes from jeffrey richter
From the excellent book Pro C# 5 and the .NET 4.5 Framework:
When a *.dll or *.exe has been created using a .NET-aware compiler, the binary blob is termed an
assembly.
An assembly contains MSIL code, which is conceptually similar to Java bytecode in that
it is not compiled to platform-specific instructions until absolutely necessary.
Typically, “absolutely
necessary” is the point at which a block of MSIL instructions (such as a method implementation) is
referenced for use by the .NET runtime.
In addition to MSIL instructions, assemblies also contain metadata that describes in vivid detail the
characteristics of every “type” within the binary. For example, if you have a class named SportsCar, the
type metadata describes details such as SportsCar’s base class, which interfaces are implemented by
SportsCar (if any), as well as a full description of each member supported by the SportsCar type. .NET
metadata is always present within an assembly, and is automatically generated by a .NET-aware
language compiler.
Finally, in addition to MSIL and type metadata, assemblies themselves are also described using
metadata, which is officially termed a manifest. The manifest contains information about the current
version of the assembly, culture information (used for localizing string and image resources), and a list
of all externally referenced assemblies that are required for proper execution.

How to bundle a required DLL with a solution in Visual C# (Express, 2010)

I have an application written in C# which interfaces with some custom hardware using a vendor supplied .Net assembly. The .Net assembly in turn loads another DLL at run time. I can add the vendor supplied assembly to my project but when I run it, the vendor assembly complains that it can't load the required DLL. I can work around this for the moment by copying the DLL to the bin\Debug and bin\Release folder.
The problem is that I will need to distribute this application to clients at some point and they will not have this DLL in place. I can't see how I can make the solution require it; I can't add it as a reference since I get the error "A reference to foo.dll could not be added. Please make sure that the file is accessible, and that it is a valid assembly or COM component."
I can't convert the DLL to a .Net assembly using TlbExp, it gives an error "The module was expected to contain an assembly manifest."
I thought if I published the application via "click once" that I could declare the dependency there but I don't see any way for this either. I also tried adding it as a file resource but this didn't seem to help.
I can see other questions on SO relating to how to invoke functionality in an external DLL but in this case, I just need a way to bundle the DLL with the project.
Thanks.
Indicates that the attributed method is exposed by an unmanaged dynamic-link library (DLL)
The DllImportAttribute attribute provides the information needed to call a function exported from an unmanaged DLL. As a minimum requirement, you must supply the name of the DLL containing the entry point.
For further reference go here
Link to Review
You could add the dll as a resource, write it out as a byte[] to a file on loading, and do an Assembly.Load() at runtime for deployment.
You might have to use an AppDomain.AssemblyResolve Event to make sure the assembly resolves in case of multiple versions.
you could add both (all) of the dlls in your project as references and mark them as "copy local". That should do it unless the first DLL expects the second DLL in a specific place.

Steps to create singlefile and multifile assembly?

I know a single file assembly consists of Manifest + IL + Resources. There is also an assembly type that groups its elements in multiple files, called a multi-file assembly. Visual Studio .NET IDE can only be used to create single-file assemblies, but Multifile assemblies can be created using command-line compilers.
This is all the information on the two types of assemblies that I could find, and google only returns http://msdn.microsoft.com/en-us/library/z38d5bzk(v=vs.71).aspx
But I am not satisfied and I cannot clearly say what both of them actually are. I want to know:
What exactly is single file assembly? How to create, steps to create, how does it look?
What exactly is multifile assembly?How to create, steps to create, how does it look?
Basically, a "single-file" assembly is an assembly that has everything contained in one file. This will be a file that has a .DLL or .EXE extension, and is the compiled .NET project. Visual studio creates these from C# projects (either library or application).
With command line compilers, you can split an assembly into multiple parts - where a single assembly's Manifest contains the information required to find information that's part of the assembly, but stored in a separate file. For example, you can keep a resource image (ie: a .bmp) that is a large resource in its own file, so that it isn't necessary to load it just to open the assembly. Creation of multi-file assemblies is not supported by Visual Studio, but these will look like a DLL or EXE, plus zero or more netmodule files, plus zero or more resource files (which can be anything). The main DLL or EXE contains the manifest that specifies where the other files are located. The steps required to build this are detailed in How to: Build a Multifile Assembly.

What are .NET Assemblies?

What are .NET Assemblies? I browsed over the net and I am not able to understand the definition.
Assembly is the smallest unit of deployment of a .net application.
It can be a dll or an exe.
There are mainly two types to it:
Private Assembly:
The dll or exe which is sole property of one application only. It is generally stored in application root folder
Public/Shared assembly:
It is a dll which can be used by multiple applications at a time. A shared assembly is stored in GAC i.e Global Assembly Cache.
Sounds difficult? Naa....
GAC is simply C:\Windows\Assembly folder where you can find the public assemblies/dlls of all the softwares installed in your PC.
There is also a third and least known type of an assembly: Satellite Assembly.
A Satellite Assembly contains only static objects like images and other non-executable files required by the application.
In more simple terms: A chunk of (precompiled) code that can be executed by the .NET runtime environment. A .NET program consists of one or more assemblies.
In addition to the accepted answer, I want to give you an example!
For instance, we all use
System.Console.WriteLine()
But Where is the code for System.Console.WriteLine!?
which is the code that actually puts the text on the console?
If you look at the first page of the documentation for the Console class, you‘ll see near the top the following: Assembly: mscorlib (in mscorlib.dll)
This indicates that the code for the Console class is located in an assem-bly named mscorlib. An assembly can consist of multiple files, but in this case it‘s only one file, which is the dynamic link library mscorlib.dll.
The mscorlib.dll file is very important in .NET, It is the main DLL for class libraries in .NET, and it contains all the basic .NET classes and structures.
if you know C or C++, generally you need a #include directive at the top that references a header file. The include file provides function prototypes to the compiler. on the contrast The C# compiler does not need header files. During compilation, the C# compiler access the mscorlib.dll file directly and obtains information from metadata in that file concerning all the classes and other types defined therein.
The C# compiler is able to establish that mscorlib.dll does indeed contain a class named Console in a namespace named System with a method named WriteLine that accepts a single argument of type string.
The C# compiler can determine that the WriteLine call is valid, and the compiler establishes a reference to the mscorlib assembly in the executable.
by default The C# compiler will access mscorlib.dll, but for other DLLs, you‘ll need to tell the compiler the assembly in which the classes are located. These are known as references.
I hope that it's clear now!
From DotNetBookZero Charles pitzold
Wikipedia has to say:
In the Microsoft .NET framework, an
assembly is a partially compiled code
library for use in deployment,
versioning and security. There are two
types: process assemblies (EXE) and
library assemblies (DLL). A process
assembly represents a process which
will use classes defined in library
assemblies. .NET assemblies contain
code in CIL, which is usually
generated from a CLI language, and
then compiled into machine language at
runtime by the CLR just-in-time
compiler. An assembly can consist of
one or more files. Code files are
called modules. An assembly can
contain more than one code module and
since it is possible to use different
languages to create code modules it is
technically possible to use several
different languages to create an
assembly. Visual Studio however does
not support using different languages
in one assembly.
If you really did browse it would help if you'd clarify what you don't understand
See this:
In the Microsoft .NET framework, an assembly is a partially compiled code library for use in deployment, versioning and security
physical collection of Class, interface, enum etc which is in IL code. Which can be .EXE or .DLL file .EXE is executable file and .DLL can dynamically used in any .net Supported language.
An assembly is a collection of types and resources that forms a logical unit of functionality. All types in the .NET Framework must exist in assemblies; the common language runtime does not support types outside of assemblies. Each time you create a Microsoft Windows® Application, Windows Service, Class Library, or other application with Visual Basic .NET, you're building a single assembly. Each assembly is stored as an .exe or .dll file.
Source : https://msdn.microsoft.com/en-us/library/ms973231.aspx#assenamesp_topic4
For those with Java background like me hope following diagram clarifies concepts -
Assemblies are just like jar files (containing multiple .class files). Your code can reference an existing assemblie or you code itself can be published as an assemblie for other code to reference and use (you can think this as jar files in Java that you can add in your project dependencies).
At the end of the day an assembly is a compiled code that can be run on any operating system with CLR installed. This is same as saying .class file or bundled jar can run on any machine with JVM installed.
MSDN has a good explanation:
Assemblies are the building blocks of .NET Framework applications; they
form the fundamental unit of
deployment, version control, reuse,
activation scoping, and security
permissions. An assembly is a
collection of types and resources that
are built to work together and form a
logical unit of functionality. An
assembly provides the common language
runtime with the information it needs
to be aware of type implementations.
To the runtime, a type does not exist
outside the context of an assembly.
In .Net, an assembly can be:
A collection of various manageable parts containing Types (or
Classes), Resources (Bitmaps/Images/Strings/Files), Namespaces,
Config Files compiled Privately or Publicly; deployed to a
local or Shared (GAC) folder; discover-able by other
programs/assemblies and; can be version-ed.
As assembly is the smallest unit of versioning security, deployment and reusability of code in Microsoft.Net.
It contains:
- Assembly Identity
- Manifest
- Metadata
- MSIL Code
- Security Information
- Assembly Header
Assembly is the fundamental part of programming with .NET Framework.
It contain code that CLR executes MSIL(Microsoft Intermediate Language) code in a portable executable file will not be executed if it does not have an associated assembly manifest.
In .NET, when we compile our source code then assembly gets generated in Visual Studio. Assembly consists of two parts Manifest and IL(Intermediate Language). Manifest contains assembly metadata means assembly's version requirements, security identity, names and hashes of all files that make up the assembly. IL contains information about classes, constructors, main method etc.
Visual Studio solutions consists of one or more projects. For Example : Console projects can produce an assembly. An assembly is logically chunk of code that can be shipped to customers, and physically an .EXE (executable program) or .DLL (are reusable by other programs).

Categories