Executing a method without using interfaces or references does have uses. I want the option to use SqlServerCe (*.sdf) databases in my application. I can use the system.data.common namespace for everything except the inititial creation of a new blank database. I would not reference a dll that I might not use just so I could have the option of creating a new sdf database. Below is the code that I am using to create a new database without an interface or a reference.
Code:Imports System.Reflection Public Class CreateSDF Public Shared Sub CreateDatabase(ByVal SqlServerCe_FileName As String, ByVal ConnectionString As String) Dim assembly As System.Reflection.Assembly = System.Reflection.Assembly.LoadFrom(SqlServerCe_FileName) CreateDatabase(assembly, ConnectionString) End Sub Private Shared Sub CreateDatabase(ByVal SqlServerCe_Assembly As System.Reflection.Assembly, ByVal ConnectionString As String) Dim expectedFullname As String = "System.Data.SqlServerCe, Version=3.5.1.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" If SqlServerCe_Assembly.FullName <> expectedFullname Then Throw New Exception("The assembly passed in is not the one I want. I want this one--> " & expectedFullname) Exit Sub End If Dim ClassNameType As String = "System.Data.SqlServerCe.SqlCeEngine" Dim methodname As String = "CreateDatabase" Dim typ As Type = Nothing typ = SqlServerCe_Assembly.GetType(ClassNameType) Dim inst As Object = Activator.CreateInstance(typ, {ConnectionString}) For Each m As MethodInfo In inst.GetType.GetMethods If m.Name = methodname Then m.Invoke(inst, {}) End If Next End Sub End Class




Reply With Quote