none
Activator.CreateInstance. Create an instance of newest object version RRS feed

  • Question

  • Hello
    I want to have ability to create any object from any assembly. For this purpose i use such code as:
    Object conn = Activator.CreateInstance("System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", "System.Data.OleDb.OleDbConnection").Unwrap();

    But i want to create newest version of the object from the assembly with biggest version. I want something like
    Object conn = Activator.CreateInstance("System.Data", "System.Data.OleDb.OleDbConnection").Unwrap();

    But i cant find a way to do that. 

    Thank you



    Saturday, March 6, 2010 5:46 PM

Answers

  • You'll need to look through the assemblies is the GAC and determine the full name (including the version number) of the assembly.

    Unfortunately, there is no managed API for this :(

    Here are wrappers by Junfeng Zhang (MSFT).  Use the AssemblyCacheEnum class.

    http://blogs.msdn.com/junfeng/articles/229649.aspx

    Hopefully, System.Data is just an arbitrary example.  There is no need to do this for the assemblies that come with .NET.  Specify the version number that is actually on your system right now.  The runtime will automatically "do the right thing".  Changes to the assembly version numbers have been very rare, and when they have happened (.NET versions 2.0 and 4.0), the runtime automatically upgrades the references for the old versions because it has special knowledge of what to do concerning assemblies included with .NET.  (This process is known as "unification".)

    BTW, what you are trying to doing is against the philosophy of the GAC (FWIW, the design of the GAC is not perfect, and a lot of people don't buy into its philosophy anyway).  The built-in ways of dealing with this are to use bindingRedirect elements in the app.config file (overrides for one application) or publisher policy assemblies (overrides globally).
    • Marked as answer by GarF1eld Saturday, March 6, 2010 11:42 PM
    Saturday, March 6, 2010 6:01 PM

All replies

  • You'll need to look through the assemblies is the GAC and determine the full name (including the version number) of the assembly.

    Unfortunately, there is no managed API for this :(

    Here are wrappers by Junfeng Zhang (MSFT).  Use the AssemblyCacheEnum class.

    http://blogs.msdn.com/junfeng/articles/229649.aspx

    Hopefully, System.Data is just an arbitrary example.  There is no need to do this for the assemblies that come with .NET.  Specify the version number that is actually on your system right now.  The runtime will automatically "do the right thing".  Changes to the assembly version numbers have been very rare, and when they have happened (.NET versions 2.0 and 4.0), the runtime automatically upgrades the references for the old versions because it has special knowledge of what to do concerning assemblies included with .NET.  (This process is known as "unification".)

    BTW, what you are trying to doing is against the philosophy of the GAC (FWIW, the design of the GAC is not perfect, and a lot of people don't buy into its philosophy anyway).  The built-in ways of dealing with this are to use bindingRedirect elements in the app.config file (overrides for one application) or publisher policy assemblies (overrides globally).
    • Marked as answer by GarF1eld Saturday, March 6, 2010 11:42 PM
    Saturday, March 6, 2010 6:01 PM
  • Hopefully, System.Data is just an arbitrary example.  There is no need to do this for the assemblies that come with .NET.  Specify the version number that is actually on your system right now.  The runtime will automatically "do the right thing".  Changes to the assembly version numbers have been very rare, and when they have happened (.NET versions 2.0 and 4.0), the runtime automatically upgrades the references for the old versions because it has special knowledge of what to do concerning assemblies included with .NET.  (This process is known as "unification".)
    The assembly i meant was Ibm.Data.Db2.dll. I dont know which version of db2 is installed on the client, so i want to create class from any existing (since i dont reference that library)

    I've read link, and i'm interested in using fusion.dll for that purpose. Actually my dll project is written in с++\cli so i can use fusion. But i have a question: is fusion.dll installed on every machine with .net framework?
    Saturday, March 6, 2010 6:38 PM
  • >  is fusion.dll installed on every machine with .net framework?

    Yes, this is a critical component of the .NET Framework.
    Saturday, March 6, 2010 10:56 PM