Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Use GAC Assembly.

Use GAC Assembly.

Scheduled Pinned Locked Moved C#
csharptutorialdotnetannouncement
11 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rahul RK
    wrote on last edited by
    #1

    Hello Friends, I want to know how to use assembly which is registered in GAC(Global Assembly Cache). I have Created an assembly name dellMe.dll and i have registered it with same name but different version 1.0.0.0 abd 1.0.0.1. Can i load the any one of the assembly at runtime. Basically, i want to know how to do this refering of assembly in our C#.Net application. Any Example will be helpfull. Thanks In Advance. Rahul Kulkarni

    J 1 Reply Last reply
    0
    • R Rahul RK

      Hello Friends, I want to know how to use assembly which is registered in GAC(Global Assembly Cache). I have Created an assembly name dellMe.dll and i have registered it with same name but different version 1.0.0.0 abd 1.0.0.1. Can i load the any one of the assembly at runtime. Basically, i want to know how to do this refering of assembly in our C#.Net application. Any Example will be helpfull. Thanks In Advance. Rahul Kulkarni

      J Offline
      J Offline
      jdkulkarni
      wrote on last edited by
      #2

      You can do it using reflection.Assembly asm = Assembly.Load("System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
      :)

      Jayant D. Kulkarni Brainbench Certified Software Engineer in C#, ASP.NET, .NET Framework and ADO.NET

      R 1 Reply Last reply
      0
      • J jdkulkarni

        You can do it using reflection.Assembly asm = Assembly.Load("System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
        :)

        Jayant D. Kulkarni Brainbench Certified Software Engineer in C#, ASP.NET, .NET Framework and ADO.NET

        R Offline
        R Offline
        Rahul RK
        wrote on last edited by
        #3

        For this i have to copy the assembly. Right? how can i keep different versions of assembly of same name? Thanks. Rahul Kulkarni

        J 1 Reply Last reply
        0
        • R Rahul RK

          For this i have to copy the assembly. Right? how can i keep different versions of assembly of same name? Thanks. Rahul Kulkarni

          J Offline
          J Offline
          jdkulkarni
          wrote on last edited by
          #4

          If you check the code carefully then you will come to know that System.Data is from GAC only. Now I have .NET 1.1 and .NET 2.0 installed on my machine. Both contains System.Data but with different versions. That is the speciality of GAC. You need to create a strong name and change the version. Then you can copy your assembly in GAC. You do not need to copy it to your application.

          Jayant D. Kulkarni Brainbench Certified Software Engineer in C#, ASP.NET, .NET Framework and ADO.NET

          R 2 Replies Last reply
          0
          • J jdkulkarni

            If you check the code carefully then you will come to know that System.Data is from GAC only. Now I have .NET 1.1 and .NET 2.0 installed on my machine. Both contains System.Data but with different versions. That is the speciality of GAC. You need to create a strong name and change the version. Then you can copy your assembly in GAC. You do not need to copy it to your application.

            Jayant D. Kulkarni Brainbench Certified Software Engineer in C#, ASP.NET, .NET Framework and ADO.NET

            R Offline
            R Offline
            Rahul RK
            wrote on last edited by
            #5

            Thanks ....It works nice. Thank You Very much Rahul Kulkarni

            1 Reply Last reply
            0
            • J jdkulkarni

              If you check the code carefully then you will come to know that System.Data is from GAC only. Now I have .NET 1.1 and .NET 2.0 installed on my machine. Both contains System.Data but with different versions. That is the speciality of GAC. You need to create a strong name and change the version. Then you can copy your assembly in GAC. You do not need to copy it to your application.

              Jayant D. Kulkarni Brainbench Certified Software Engineer in C#, ASP.NET, .NET Framework and ADO.NET

              R Offline
              R Offline
              Rahul RK
              wrote on last edited by
              #6

              As per you said all working fine. even we can create instatance of the class of the assembly. How can we call function of that class. It is not giving intellisense. what is the solution.? Rahul Kulkarni.

              J 2 Replies Last reply
              0
              • R Rahul RK

                As per you said all working fine. even we can create instatance of the class of the assembly. How can we call function of that class. It is not giving intellisense. what is the solution.? Rahul Kulkarni.

                J Offline
                J Offline
                jdkulkarni
                wrote on last edited by
                #7

                Can do like this.Assembly asm = Assembly.Load("System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"); Type[] tps = asm.GetTypes(); foreach (Type tp in tps) { if (tp.IsClass) { MethodInfo[] methods = tp.GetMethods(); object abc = Activator.CreateInstance(tp); if (!methods[0].IsStatic) { methods[0].Invoke(tp, new object[] { }); } } }
                :)

                Jayant D. Kulkarni Brainbench Certified Software Engineer in C#, ASP.NET, .NET Framework and ADO.NET

                1 Reply Last reply
                0
                • R Rahul RK

                  As per you said all working fine. even we can create instatance of the class of the assembly. How can we call function of that class. It is not giving intellisense. what is the solution.? Rahul Kulkarni.

                  J Offline
                  J Offline
                  jdkulkarni
                  wrote on last edited by
                  #8

                  Here is a sample code that will open a connection to sql server using reflection.string connString = "Server = localhost; Database = EmployeePortal; User ID = abc; Password = abc; Trusted_Connection = False;"; Assembly asm = Assembly.Load("System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"); Type[] tps = asm.GetTypes(); foreach (Type tp in tps) { if (tp.IsClass) { if (tp.Name == "SqlConnection") { Console.WriteLine("Sql connection found..."); } MethodInfo[] methods = tp.GetMethods(); object abc = null; try { abc = Activator.CreateInstance(tp, connString); } catch (Exception ex) { } foreach (MethodInfo method in methods) { if (tp.Name == "SqlConnection" && method.Name == "Open") { Console.WriteLine(method.Name); method.Invoke(abc, null); } } } }
                  :)

                  Jayant D. Kulkarni Brainbench Certified Software Engineer in C#, ASP.NET, .NET Framework and ADO.NET

                  R 1 Reply Last reply
                  0
                  • J jdkulkarni

                    Here is a sample code that will open a connection to sql server using reflection.string connString = "Server = localhost; Database = EmployeePortal; User ID = abc; Password = abc; Trusted_Connection = False;"; Assembly asm = Assembly.Load("System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"); Type[] tps = asm.GetTypes(); foreach (Type tp in tps) { if (tp.IsClass) { if (tp.Name == "SqlConnection") { Console.WriteLine("Sql connection found..."); } MethodInfo[] methods = tp.GetMethods(); object abc = null; try { abc = Activator.CreateInstance(tp, connString); } catch (Exception ex) { } foreach (MethodInfo method in methods) { if (tp.Name == "SqlConnection" && method.Name == "Open") { Console.WriteLine(method.Name); method.Invoke(abc, null); } } } }
                    :)

                    Jayant D. Kulkarni Brainbench Certified Software Engineer in C#, ASP.NET, .NET Framework and ADO.NET

                    R Offline
                    R Offline
                    Rahul RK
                    wrote on last edited by
                    #9

                    Firstly, Thanks. You helped me alot. I got the concept you are telling. On the basis of your example, i tried for messagebox function. But it is giving me exception "Parameter Missmatch". I am not getting were do i can function returned value Rahul Kulkarni

                    J 1 Reply Last reply
                    0
                    • R Rahul RK

                      Firstly, Thanks. You helped me alot. I got the concept you are telling. On the basis of your example, i tried for messagebox function. But it is giving me exception "Parameter Missmatch". I am not getting were do i can function returned value Rahul Kulkarni

                      J Offline
                      J Offline
                      jdkulkarni
                      wrote on last edited by
                      #10

                      If you have overloaded methods then you need to check which method you want to execute. Here is the sample code to call MessageBox.Show("Hi").Assembly winAsm = Assembly.Load("System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"); Type[] allTyps = winAsm.GetTypes(); foreach (Type typ in allTyps) { if (typ.Name == "MessageBox") { Console.WriteLine("MessageBox found..."); MethodInfo[] mtds = typ.GetMethods(); foreach (MethodInfo mtd in mtds) { if (mtd.Name == "Show") { if (mtd.GetParameters().Length == 1) { mtd.Invoke(typ, new object[] { "Hi!!" }); } } } } }
                      :)

                      Jayant D. Kulkarni Brainbench Certified Software Engineer in C#, ASP.NET, .NET Framework and ADO.NET

                      R 1 Reply Last reply
                      0
                      • J jdkulkarni

                        If you have overloaded methods then you need to check which method you want to execute. Here is the sample code to call MessageBox.Show("Hi").Assembly winAsm = Assembly.Load("System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"); Type[] allTyps = winAsm.GetTypes(); foreach (Type typ in allTyps) { if (typ.Name == "MessageBox") { Console.WriteLine("MessageBox found..."); MethodInfo[] mtds = typ.GetMethods(); foreach (MethodInfo mtd in mtds) { if (mtd.Name == "Show") { if (mtd.GetParameters().Length == 1) { mtd.Invoke(typ, new object[] { "Hi!!" }); } } } } }
                        :)

                        Jayant D. Kulkarni Brainbench Certified Software Engineer in C#, ASP.NET, .NET Framework and ADO.NET

                        R Offline
                        R Offline
                        Rahul RK
                        wrote on last edited by
                        #11

                        Thanks very much..

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups