Use GAC Assembly.
-
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
-
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
-
-
For this i have to copy the assembly. Right? how can i keep different versions of assembly of same name? Thanks. Rahul Kulkarni
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
-
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
-
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
-
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.
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
-
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.
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
-
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
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
-
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
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
-
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