How to Register an Assembly or DLL in Web Applications

Question

I have a System.Web.DataVisualization.dll present in my bin folder. Now I have added the registration tag to my page as below:

  1. <%@ Register Assembly="System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="System.Web.UI.DataVisualization.Charting" TagPrefix="asp" %>
Now how to deermine the Assembly and Namespace from System.Web.DataVisualization.dll?

Answer
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Reflection;  
  8. using System.Threading;  
  9. public partial class _Default : System.Web.UI.Page  
  10. {  
  11.     protected void Page_Load(object sender, EventArgs e)  
  12.     {  
  13.         Assembly[] myAssemblies = Thread.GetDomain().GetAssemblies();  
  14.         Assembly myAssembly = null;  
  15.         for (int i = 0; i < myAssemblies.Length; i++)  
  16.         {  
  17.             if (String.Compare(myAssemblies[i].GetName().Name,  
  18.                               "System.Web.DataVisualization") == 0)  
  19.             {  
  20.                 myAssembly = myAssemblies[i];  
  21.                 break;  
  22.             }  
  23.         }  
  24.         if (myAssembly != null)  
  25.         {  
  26.             //This will give the Version Information.  
  27.             var myVersion = myAssembly.GetName().Version;  
  28.             //This will give the Namespace Infromation.  
  29.             var namespaces = myAssembly.GetTypes()  
  30.                          .Select(t => t.Namespace)  
  31.                          .Distinct();  
  32.         }  
  33.     }  
  34. }

1. You can find the Version Information from the following code statement.

  1. var myVersion = myAssembly.GetName().Version;  
Image 1.jpg

2. You can also find the Namespace Information from the following code statement:

  1. var namespaces = myAssembly.GetTypes()  
  2.                          .Select(t => t.Namespace)  
  3.                          .Distinct(); 

Image 2.jpg

3. So I can generate the register tag as follows:

  1. <%@ Register Assembly="System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="System.Web.UI.DataVisualization.Charting" TagPrefix="asp" %>


Similar Articles