-1

For an auto-update scenario, I'd like app to automatically pull down an updated dll from the cloud. In my VS project, I have an assembly (v1.0.0.0) referenced. I'd like to load a newer version (for example v1.1.0.0) of it at runtime, so that when I new it up:

var class1 = new Class1(); 

I'd like to get an instance of type (Class1) from assembly version v1.1.0.0. Is this even possible?

Note: I'm not looking a way to make an instance type Class1 with reflection, but newing it up.

5
  • 1
    Your question is quite unclear. Commented Nov 12, 2014 at 19:49
  • You can setup the application config to redirect version bindings. Commented Nov 12, 2014 at 19:50
  • you're going to get the class that your references indicate, if you wanted a specific version then you should load the specific assembly and load that type via CreateInstanceAndUnwrap() Commented Nov 12, 2014 at 19:51
  • You would have to do an assembly redirect. Commented Nov 12, 2014 at 20:17
  • @Pompair hi OP, could you have a look at this question please?stackoverflow.com/questions/67177542/… Commented Apr 21, 2021 at 7:00

1 Answer 1

2

You need to use assembly redirection to tell the loader to load a newer (but still compatible) assembly for your application when it's binding assemblies. This is something that you do in app.config or web.config, depending on the application type. For example (taken from the link above):

<dependentAssembly> <assemblyIdentity name="someAssembly" publicKeyToken="32ab4ba45e0a69a1" culture="en-us" /> <bindingRedirect oldVersion="7.0.0.0" newVersion="8.0.0.0" /> </dependentAssembly> 

oldVersion allows for a version range, so multiple older versions can be mapped to a single new version of an assembly. Also, multiple bindingRedirect entries may be present.

Edit:

If you want to dynamically load a type from an assembly, you cannot reference the type itself (that is, you cannot have something like Class1 obj = ...; (var obj = ... is the same) because this ties the type to the one seen by the compiler. Assembly redirection would work but if you don't know the right version, you cannot use it.

Another option is to define interfaces in your main application and then have the various types in various assemblies implement these interfaces. This way you can dynamically load a type and cast it to a known interface type. For this, you can use either dependency injection (like LightInject, etc.) or you can have a config file that lists what assemblies contain a particular implementation of an interface. At the end you'd do something like:

IInterface obj = (IInterface) Activator.CreateInstance ( "assemblyname", "typename" ).Unwrap (); 

This assumes that assemblyname refers to a known assembly that can be loaded and that typename is in that assembly and it implements IInterface.

Using interfaces ensures that the only hardcoded type at compile time is the interface and you can load any implementation from any assembly, as long as those implementations implement the right interface. This makes it easy to swap out various components.

Using a DI library, much of this is done for you - the various libraries deal with discovering types implementing interfaces in assemblies and then they give you a new instance when you request one.

You can also look at this answer to a similar question for some extra information: https://stackoverflow.com/a/26809961/682404

Sign up to request clarification or add additional context in comments.

6 Comments

This I cannot use since this would mean I'd need to know the future version number of the dll beforehand and I'd have to have access to client config file. This luxyry I don't have. The app is out in the field at users and I want to provide a patch dll via server. To the app checks for an update at app start etc. that's the easy part, but how to replace the referenced dll within the app?
@Pompair I added some details to the answer.
+1. @Pompair, you don't have to know the future version number. Just make sure that the redirect statement goes to some crazy high number like v999.999.999. Should work fine, at that point.
@xxbbcc could you have a look at this question please?
@slowjams I'm sorry, what question do you mean?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.