You are on page 1of 23

What is a .Net Assembly?

Assembly is deployment of unit of .Net Framework. It may be executable (.exe) or dynamic link library (.dll) file.It Consists of Assembly type, data type, IL code and resources. In .Net there are three type of assembly. 1.Private Assembly. 2.Shared Assembly. 3.Satelite Assembly

Private Assembly The Private Assembly is stored in particular folder. The disadvantage is that each
time a copy of the Assembly is created which in turn occupies more memory and reduces the performance.

Shared Assembly Shared assembly is stored in GAC.More than one application can share one
assembly. use of memory can be reduced by this concept.here public key and version number to maintain unique ness of the Assembly forparticular application

Satellite Assembly When we make the application customizable for different languages and cultures
then distribute the localized modules in separate assemblies called satellite assemblies.

How create private assembly and usages it?


Step 1 : File->New->Project->Class Library->Add Step2 : create new class Calculator

using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Calculator { public class MathAddition

{ public int Addition(int a, int b) { int c = a + b; return (c); } } }

Step 3 :create new console application . Step 4:Add reference of dll Step 5:Create namespace.

using System; using System.Collections.Generic; using System.Linq; using System.Text; using Calculator; namespace ConsoleApplication12 { class Program { static void Main(string[] args) { MathAddition obj = new MathAddition(); int c=obj.Addition(100, 100); Console.WriteLine(c); Console.ReadLine(); }

} }

how create global assembly and usages it? (C:\Windows\Microsoft.NET\assembly\GAC_MSIL)


Step 1 : Open Visual Studio .NET Step 2 : Generating Cryptographic Key Pair using the tool SN.Exe sn -k "C:\test\test.snk" Step 3 : Sign the component with the key <Assembly: AssemblyKeyFile("C:\mykeys\mykeys.key")> step 4 : Host the signed assembly in Global Assembly Cache gacutil -I "C:\[PathToBinDirectoryInVSProject]\myGAC.dll" Step 5 : Test the assembly

Reflection Reflection is used to find assemblies, modules and type information at runtime Or reflection
provides objects(Type) through which we can encapsulate assemblies, modules and types. Here's a simple example of Reflection using the static method GetType - inherited by all types from the Object base class - to obtain the type of a variable: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Ace_A_MetricDLLTest { class Program { static void Main(string[] args) { int i = 42;

System.Type type = i.GetType(); System.Console.WriteLine(type); Console.ReadLine(); } } }

To Show all assembly present current Application domain


using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection;

namespace ConsoleApplication2 { class Program { static void Main(string[] args) { AppDomain MyDomain = AppDomain.CurrentDomain; Assembly[] AssembliesLoaded = MyDomain.GetAssemblies(); foreach (Assembly MyAssembly in AssembliesLoaded) { Console.WriteLine(MyAssembly.FullName + " ");

} Console.Read();

} } }

To load assembly by location


using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection;

namespace ConsoleApplication2 { class Program { static void Main(string[] args) { System.Reflection.Assembly o = System.Reflection.Assembly.LoadFrom("C:\\Windows\\Microsoft.NET\\assembly\\GAC_MSIL\\Ace-AMetricDll\\v4.0_1.0.0.0__7494c08d277bf8e3\\Ace-A-MetricDll.dll"); Console.Write(o.GetName()); Console.Read();

} } }

To load assembly by assembly name


using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection;

namespace ConsoleApplication2 { class Program { static void Main(string[] args) { System.Reflection.Assembly o = System.Reflection.Assembly.Load("Ace-A-MetricDll, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7494c08d277bf8e3"); Console.Write(o.GetName()); Console.Read();

} } }

Program to show all method present in public assembly.


using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection;

namespace ConsoleApplication2 { class Program { static void Main(string[] args) { System.Reflection.Assembly o = System.Reflection.Assembly.Load("Ace-A-MetricDll, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7494c08d277bf8e3"); Type[] types = o.GetTypes(); foreach (Type t in types) { MethodInfo[] methods = t.GetMethods(); foreach (MethodInfo method in methods) { Console.WriteLine(method.Name); } } Console.Read();

} } }

Program to call method of public assembly


using System; using System.Collections.Generic; using System.Linq; using System.Text;

using System.Reflection;

namespace ConsoleApplication2 { class Program { static void Main(string[] args) { System.Reflection.Assembly o = System.Reflection.Assembly.Load("Ace-A-MetricDll, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7494c08d277bf8e3"); Type types = o.GetTypes()[0]; object instance = Activator.CreateInstance(types); MethodInfo method = types.GetMethod("getData"); object[] parameters = null; Console.WriteLine(method.Invoke(instance, parameters)); Console.Read();

} } }

Assembly & Reflection

What is a .Net Assembly?


Assembly is deployment of unit of .Net Framework. It may be executable (.exe) or dynamic link library (.dll) file.It Consists of Assembly type, data type, IL code and resources. In .Net there are three type of assembly. 1.Private Assembly. 2.Shared Assembly. 3.Satelite Assembly

Private Assembly The Private Assembly is stored in particular folder. The disadvantage is that each
time a copy of the Assembly is created which in turn occupies more memory and reduces the performance.

Shared Assembly

Shared assembly is stored in GAC.More than one application can share one assembly. use of memory can be reduced by this concept.here public key and version number to maintain unique ness of the Assembly forparticular application

Satellite Assembly When we make the application customizable for different languages and cultures
then distribute the localized modules in separate assemblies called satellite assemblies.

How create private assembly and usages it? how create global assembly and usages it?
Step 1 : Open Visual Studio .NET Step 2 : Generating Cryptographic Key Pair using the tool SN.Exe sn -k "C:\test\test.snk" Step 3 : Sign the component with the key <Assembly: AssemblyKeyFile("C:\mykeys\mykeys.key")> step 4 : Host the signed assembly in Global Assembly Cache gacutil -I "C:\[PathToBinDirectoryInVSProject]\myGAC.dll"

Step 5 : Test the assembly

Reflection Reflection is used to find assemblies, modules and type information at runtime Or reflection
provides objects(Type) through which we can encapsulate assemblies, modules and types. Here's a simple example of Reflection using the static method GetType - inherited by all types from the Object base class - to obtain the type of a variable: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Ace_A_MetricDLLTest { class Program { static void Main(string[] args) { int i = 42; System.Type type = i.GetType(); System.Console.WriteLine(type); Console.ReadLine(); } } }

To Show all assembly present current Application domain


using System;

using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection;

namespace ConsoleApplication2 { class Program { static void Main(string[] args) { AppDomain MyDomain = AppDomain.CurrentDomain; Assembly[] AssembliesLoaded = MyDomain.GetAssemblies(); foreach (Assembly MyAssembly in AssembliesLoaded) { Console.WriteLine(MyAssembly.FullName + " ");

} Console.Read();

} } }

To load assembly by location


using System; using System.Collections.Generic; using System.Linq;

using System.Text; using System.Reflection;

namespace ConsoleApplication2 { class Program { static void Main(string[] args) { System.Reflection.Assembly o = System.Reflection.Assembly.LoadFrom("C:\\Windows\\Microsoft.NET\\assembly\\GAC_MSIL\\Ace-AMetricDll\\v4.0_1.0.0.0__7494c08d277bf8e3\\Ace-A-MetricDll.dll"); Console.Write(o.GetName()); Console.Read();

} } }

To load assembly by assembly name


using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection;

namespace ConsoleApplication2 { class Program {

static void Main(string[] args) { System.Reflection.Assembly o = System.Reflection.Assembly.Load("Ace-A-MetricDll, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7494c08d277bf8e3"); Console.Write(o.GetName()); Console.Read();

} } }

Program to show all method present in public assembly.


using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection;

namespace ConsoleApplication2 { class Program { static void Main(string[] args) { System.Reflection.Assembly o = System.Reflection.Assembly.Load("Ace-A-MetricDll, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7494c08d277bf8e3"); Type[] types = o.GetTypes(); foreach (Type t in types) {

MethodInfo[] methods = t.GetMethods(); foreach (MethodInfo method in methods) { Console.WriteLine(method.Name); } } Console.Read();

} } }

Program to call method of public assembly


using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection;

namespace ConsoleApplication2 { class Program { static void Main(string[] args) { System.Reflection.Assembly o = System.Reflection.Assembly.Load("Ace-A-MetricDll, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7494c08d277bf8e3"); Type types = o.GetTypes()[0]; object instance = Activator.CreateInstance(types);

MethodInfo method = types.GetMethod("getData"); object[] parameters = null; Console.WriteLine(method.Invoke(instance, parameters)); Console.Read();

} } }

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Exception Handling in C#
Exception handling is an in built mechanism in .NET framework to detect and handle run time errors.C# provides three keywords try, catch and finally to do exception handling. The general form try-catch-finally in C# is shown below

C#: Exception Handling


using System; class MyClient { public static void Main() { int x = 0; int div = 0; try

{ div = 100 / x; Console.WriteLine("This line in not executed"); } catch (DivideByZeroException de) { Console.WriteLine("Exception occured"); } Console.WriteLine("Result is {0}", div); Console.ReadLine(); } }

User-defined Exceptions In C#, it is possible to create our own exception class. But Exception
must be the ultimate base class for all exceptions in C#. So the user-defined exception classes must inherit from either Exception class or one of its standard derived classes. using System; class MyException : Exception { public MyException(string str) { Console.WriteLine(str); } } class MyClient { public static void Main() { try

{ throw new MyException("UserDefine Exception"); } catch (Exception e) { Console.WriteLine(e.ToString()); } Console.ReadLine(); } }


-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

Class With Property


using System;

Here code is showing class with property.

using System.Collections.Generic; using System.Linq; using System.Text;

namespace ConsoleApplication1 { class Employee { public int _Id; public int Id { get {

return _Id; } set { _Id=value; } }

} class Program { static void Main(string[] args) { Employee obj = new Employee(); obj.Id = 100; Console.WriteLine(obj.Id); Console.Read(); } } }

Class With Auto-Implemented Properties Here code is showing class with Auto property. In C#
3.0 and later, auto-implemented properties make property-declaration more concise when no additional logic is required in the property accessors. They also enable client code to create objects. When you declare a property as shown in the following example, the compiler creates a private, anonymous backing field that can only be accessed through the property's get and set accessors.

using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace ConsoleApplication1 { class Employee { //in C# 3.0 Auto Property public int Id { get; set; }

} class Program { static void Main(string[] args) { Employee obj = new Employee(); obj.Id = 100; Console.WriteLine(obj.Id); Console.Read(); } } }

NonGeneric Collection VS Generic Collection Generic collection allows us to put any kind of
object inside it but Non-generic collection let us put everything in the form of object system.Object.

1.Example of ArrayList(Non-generic collection)


array in and data is stored in the form object. using System; using System.Collections; using System.Collections.Generic;

It act as dynamic

using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { ArrayList lst = new ArrayList(); lst.Add(100); lst.Add(200); lst.Remove(100); foreach(int a in lst) { Console.WriteLine(a); } Console.WriteLine(lst.Count); Console.Read(); } } }

2. Example of HashTable(Non-generic collection)


value pair in the form of object.

It is used to store key

using System; using System.Collections; using System.Collections.Generic; using System.Linq;

using System.Text; namespace ConsoleApplication1 {

class Program { static void Main(string[] args) { Hashtable lst = new Hashtable(); lst.Add(1, "MicroSoft"); lst.Add(2, "Intermediate"); lst.Add(3, "Language"); foreach(int key in lst.Keys) { Console.WriteLine(key+ " } " + lst[key]);

Console.Read(); } } }

3. Example of List (Generic collection)


C# language.

List is dynamic generic arrays in the

using System; using System.Collections.Generic;

using System.Linq; using System.Text; namespace ConsoleApplication1 {

class Program { static void Main(string[] args) { List<int> lst = new List<int>(); lst.Add(10); lst.Add(20); lst.Add(30); foreach(int data in lst) { Console.WriteLine(data); }

Console.Read(); } } }

4. Example of Stack (Generic collection)

Represents a variable size last-infirst-out (LIFO) collection of instances of the same arbitrary type.

using System; using System.Collections.Generic;

using System.Linq; using System.Text; namespace ConsoleApplication1 {

class Program { static void Main(string[] args) { Stack<int> lst = new Stack<int>(); lst.Push(10); lst.Push(20); lst.Push(20); while (lst.LongCount<int>() != 0) { Console.WriteLine(lst.Pop()); }

Console.Read(); } } }

You might also like