Lesson 7: Assemblies and pointers in use





Xamarin! Reply to all the IDE’s

Assembly

An assembly,in its simplest from,can be defined as a pre-compiled logically sound code,which can be used for various purposes through the application.As the code is already compiled in .exe (Executables) or .dll (Dynamic Link Library) form;it is only loaded in to memory when it is needed,otherwise not.This helps to achieve the true performance milestone in larger applications.An assembly can have multiple modules and it is called “Multi-file Assembly”
A module can be defined as the logical collection of line of code which is part of an assembly.An assembly can have different modules written in different CLR complaint languages.
“Reflection” is the best way to obtain information about assembly.

The place where an assembly is kept is known as “Global Assembly Cache”.Every computer where the CLR is installed has a machine-wide code cache called the global assembly cache. The global assembly cache stores assemblies specifically designated to be shared by several applications on the computer.Ref http://msdn.microsoft.com/en-us/library/yf1d93sz(v=vs.100).aspx
In order to follow true professional standards,an assembly must be “Strong-Named”

Static Vs Dynamic Assemblies

Static assemblies are stored permanently on the hard drive in the form of files..Net Frame is an ideal example for that

Dynamic Assemblies are not stored on the disk before execution.After the execution process starts,the dynamic assemblies are stored on the disk and then they are loaded into the computer’s memory by the compiler.
System.Reflection.Emit namespace contains the classes which are required by the compiler (or other tool) to create Dynamic Assemblies

Assembly Manifest

Assembly Manifest is not something terrifying.It is basically the metadata (data about data) about the assembly.All the information about the version,security,identity,scope(the working boundaries) of the assembly are present in the manifest.The assembly in compliance with MSIL specifications;is present either in the form of PE file such as .exe or .dll or standalone P.E file with just metadata.

The following articles will help you out in further exploring about assemblies and their usages.These are just hard and fast rules and there is no need to explain them further in my own logic.They are to be followed just as the aplhabets A,B,C,D are as it is all over and no logical is required to learn them

1.(a)How to Create and Use and Assembly (great article on msdn)?

1(b) How to Load and UnLoad an Assembly?

2.How to Build a Multi-File Assembly (msdn)?

3.How to View Assembly Contents(msdn)?

4.How to View if a File is an Assembly or Not?

5.How to Load Assembly with Reflection?

>6. Friend Assemblies

I will load extra tutorials with so many tips and tricks very soon.

Pointers in C#

Unlike its predecessors languages C and C++,the use of pointers is not that common in C# programming language

Reasons

As C# doesn’t allow the use of pointers in safe context.That practice is discouraged to use unsafe context until and unless there are issues such as accessing data structures on disk,advanced COM (Component Object Model) or invocation of methods which have pointers in them.
Pointers are of good use in situations where the programmer takes care of the data types by himself and the faster programming is required.Pointers basically point to the location of the memory space in CPU where the pointed member (field,method,class,structure) is stored.They are good in accessing Windows API’s(Application Programming Interfaces),lesser typical typecasting etc.

In an unsafe context, a type may be a pointer type, a value type, or a reference type. A pointer can be declared in the following manner:

int* p1, p2, p3; // Ok

int *p1, *p2, *p3; // do not use in C# as it was used in C

Where int is the type integer and * is the pointer notation.I don’t like to use void pointers or something like this and I don’t recommend this

void* p1;

The unsafe code

In order to work in an unsafe-context,we have to use unsafe code.The unsafe may denote a method,type,coding blocks etc.First,we have to set the option for compilation with unsafe code.
Here is a clear demonstration in msdn site:- How to compile with unsafe code?

Examples

Example1

Let me explain the example taken from http://msdn.microsoft.com/en-us/library/aa288474(v=vs.71).aspx
Let us examine the code first

// fastcopy.cs

// compile with: /unsafe

using System;

class Test

{

/ *The unsafe keyword allows pointers to be used within

the Copy() method*/

/*

A static method Copy(),takes byte[] array as first parameter,
src index as the int parameter which will represent the index of src.
There is another byte array as parameter name as dst and the index of it
will be dstIndex.There is also an integer parameter count .Let’s see them in action

*/

static unsafe void Copy(byte[] src, int srcIndex,

byte[] dst, int dstIndex, int count)

{

/*

Check to see of src has no element or index of src
is less than zero,means no first index or dst array is empty and
it has no first index,then ArgumentException should be thrown.
Exceptions are generally used to indicate any error which might have
occurred as a result of a mistake at setting the values etc.They
are the key features of the .Net compliance languages.We will discuss them
later.

*/

if (src == null || srcIndex < 0 || dst == null || dstIndex < 0 || count < 0) { throw new ArgumentException(); //-----------------------------------(Marker A) } /*assign the length of src array to srcLen variable and length of dst to datLen variable if we pass the (Marker A) exception without bring arrested (lol) or caught there .If we were caught at the Marker(A) point,we would not reach the next lines and the program would have been terminated because no arugments/parameters being supplied to the method Copy() to execute*/ int srcLen = src.Length; int dstLen = dst.Length; /*

Here comes the story about the last parameter of the method
Copy() that is ‘count’.You just think about it that it is some positive integer
value directly passed when a call to copy method will be made.
The value is set to 100.You can see the method call at Marker(B) downwards
in the Main() method.Don’t look anything else 🙂 .
We have if check to see whether the subtraction of srcIndex value from srcLen values
is not lesser than the value in ‘count’ or
dstLen – dstIndex

*/

if (srcLen – srcIndex < count || dstLen - dstIndex < count) { throw new ArgumentException(); } /*

The following fixed statement pins the location of
the src and dst objects in memory so that they will
not be moved by garbage collection.In short, the fixed
statement fixes the addresses of the src and dst byte arrays
in memory so that they can not be changed.We have to
use fixed because we are working with unsafe code…dude 🙂
Now what is (byte* pSrc=src,pDst=dst)? confused! Let me clarify this…
The arguments in the fixed statements are assigning the addresses
of the first elements of each of the objects/arrays src and dst to pSrc and pDst pointers respectively.
byte* clearly indicates that the types of pointers are the same as those of the objects.* is the pointer notation.
We have to just refer to the first element (i.e the zeroth index element ) of the array by the pointer to get our task done
Other similar notation can be fixed(byte* pSrc=&[0]src,pDst=&[0]dst) { /*…*/ }.
The & is the address of operator.Don’t confuse it with &&(AND Operator) man!!!
other examples:
int* p …. p is a pointer to an integer
int** p……p is a pointer to pointer to an integer
int*[] p…….p is a single-dimensional array of pointers to integers
char* p…….p is a pointer to a char
void* p………I hate this

Download my code file (Example-1.docx) for more clarification

*/

fixed (byte* pSrc = src, pDst = dst)

{

/* pointers ps and pd of type byte are pointing to the pointers pSrc
and pDst respectively.pSrc and pDst are pointing to the arrays src and dst respectively
as well */

byte* ps = pSrc;

byte* pd = pDst;

/* Loop over the count in blocks of 4 bytes, copying an
integer (4 bytes) at a time.
At this point,you must be thinking something like that….
What the hell is that? I can’t understand . I think programming
is too tough for me.Why I chose that :(.
It’s not your fault dear! it is the code that looks something totally strange
Download the file Example1.docx.Copy the code in the C# editor or online C# and run it
Just look at that code block where I have used fixed() especially the last line

*/
for (int n =0 ; n < count/4 ; n++) { */Just swap the values in the destination bytes array with those of source bytes array with pointer pointing to pointer pointing to arrays. Tough huh.... */ *((int*)pd) = *((int*)ps); /*Jump four address up to each of pointers pointing to the pointers which point to the arrays*/ pd += 4; ps += 4; } /* Complete the copy by moving any bytes that weren't moved in blocks of 4: The logic is modulus operator implementation and just assigning the values if remain in src to dest.Check for % by yourself */ for (int n =0; n < count%4; n++) { *pd = *ps; pd++; ps++; } } } static void Main(string[] args) { //call arrays instances with count=100 i.e the size of each byte[] a = new byte[100]; byte[] b = new byte[100]; for(int i=0; i<100; ++i) /*Assign the typecasted into bytes values of each i iteration from the loop to array a*/ a[i] = (byte)i; /*Call the copy method with src=a,srcIndex=0,dest=b,destIndex=0,count=100 don't yawn,we have already discussed Copy() method in the above lines */ Copy(a, 0, b, 0, 100);//--------------Marker(B) Console.WriteLine("The first 10 elements are:"); for(int i=0; i<10; ++i) Console.Write(b[i] + " "); Console.WriteLine("n"); } }

Example Output

The first 10 elements are:

0 1 2 3 4 5 6 7 8 9

That’s all my dear friends. I hope the tension is over:)

Lesson 7 completed…..

Copyrighted.com Registered & Protected 
31VE-IXWY-5TSV-GWKR


Syed Adeel Ahmed
Analyst, Programmer, Educationist and Blogger at Technofranchise
Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.
Syed Adeel Ahmed on FacebookSyed Adeel Ahmed on GoogleSyed Adeel Ahmed on InstagramSyed Adeel Ahmed on Twitter

Published by

Syed Adeel Ahmed

Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.