0

I don't want this : Assembly.LoadFrom(PathToAssembly).

I need to really bring the dll in the run time code to my project and then use it's classes normally without the activator class. Exactly like I load a net. dll file into my project not in run time but now I need to add during the run time.

John Saunders
  • 157,405
  • 24
  • 229
  • 388
Stav Alfi
  • 10,681
  • 15
  • 74
  • 124

4 Answers4

3

In this scenario you would follow the interface pattern. Where you create an interface class with all of the desired method stubs. You would still perform you Assembly.Load, but you then cast the resulting activated object to your interface class.

I don't have an example in c#, but this vb.net should be easy to follow.

Dim alib As Byte() = System.IO.File.ReadAllBytes(Application.StartupPath & "\\PeriodicMinimumsUtilities.dll")
Dim adeb As Byte() = System.IO.File.ReadAllBytes(Application.StartupPath & "\\PeriodicMinimumsUtilities.pdb")
Dim assembly As System.Reflection.Assembly = System.Reflection.Assembly.Load(alib, adeb)
Dim type As Type = Assembly.GetType("PeriodicMinimumsUtilities.frmPeriodicMinimumsReconcilliation")
Dim obj As Fireblade.POS.Business.IPeriodicMinimumsUtilities = Activator.CreateInstance(type)

Interface:

Public Interface IPeriodicMinimumsUtilities

    WriteOnly Property Database() As Meritsoft.GolfRez.Data.Database
    Function LoadMinimumsDataSource(ByVal v_fiscalYear As String, ByVal v_fiscalPeriod As Integer) As DataSet
    Function GetMinimumsSpent(ByVal v_fiscalYear As String, ByVal v_fiscalPeriod As Integer) As DataTable
    Function LoadFiscalPeriodsDataSource(ByVal ImposeLimit As Boolean) As DataSet

End Interface
DragonZero
  • 760
  • 4
  • 8
  • 1
    Also known as **Dependency Injection** : http://stackoverflow.com/questions/130794/what-is-dependency-injection – Dude Pascalou Apr 12 '13 at 08:17
  • This is not Dependency Injection, This is not passing in Objects a class depends on through its constructor or properties. This is just using interfaces where the implementation happens to be in a dynamically loaded dll. – James Barrass Apr 12 '13 at 08:28
  • Populating a variable with an implementation of an interface that is "not known at compile time" (or in other words, that is not a dependency of the assembly) do follow the Dependency Injection pattern, as **Martin Fowler** say here : http://martinfowler.com/articles/injection.html#FormsOfDependencyInjection – Dude Pascalou Apr 12 '13 at 08:54
1
  1. You need to reference DLL like you usually do to be able to just "use" it
  2. Then, use LoadFrom or, for example, you can embed assembly as resource, and load it from stream
Lanorkin
  • 6,802
  • 2
  • 34
  • 56
0

There is no other way to dynamically load an assembly in .Net at run time than the methods offered by Assembly. Perhaps you have a more specific problem related to the method call you mention. If so, specify it by updating your question so that the community can assist you.

Ioannis Karadimas
  • 7,296
  • 3
  • 32
  • 45
0

You can't use the type information (using classes normally) and load the dll at run time, since you would have no type information to compile with.

You can use interfaces normally and load the dll against interfaces, or you can just reference the dll and use the classes normally.

A third option (but a terrible idea) would be to reflect all the classes you are trying to use, using helper methods and strings to identify properties and methods. Not a normal way of using classes but it would actualy compile without the type information being present.

James Barrass
  • 9,429
  • 4
  • 30
  • 55