Dynamic loading of an assembly


The Assembly class provides some methods to load an assembly at runtime.
    1)Load() : This is the static overloaded method which takes the assembly name as input parameter and searches the given assembly name in the system.
    2)LoadFrom() : This is the static overloaded method which takes the complete path of an assembly and searches at the specified location instead of searching in the system.
    3)GetExecutingAssembly() : This method is used to obtain the currently running assembly information.it doesn't have any parameters.
    4)GetTypes() :  This is used to obtain the details of all the types that are defined in the corresponding assembly.
    5)GetCustomAttributes() : This static overloaded method gets the attributes attached to the assembly.

    Sample:
   
     Assembly objAss;

    //must supply a valid fully qualified assembly name here.
    objAss = Assembly.Load("mscorlib,2.0.0.0,Neutral,b77a5c561934e089");

    // Loads an assembly using its file name  
    //objAssembly = Assembly.LoadFrom(@"D:\test.exe");

    //this loads currnly running process assembly
    // objAssembly = Assembly.GetExecutingAssembly();

    Type[] Types = objAss.GetTypes();

    //Display all the types contained in the specified assembly.
    foreach (Type objType in Types)
    {
          Console.WriteLine(objType.Name.ToString());
    }

No comments:

Post a Comment