Hi all!

I was wondering if there is a way to get location of assemby rather than specifying it directly at Assembly.Load(...), can we use Assembly.Load and inside the parameter, we extract the probing path from app.config?

app.config:

<configuration>

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <probing privatePath="version_control;version2;bin2\subbin;bin3"/>

      <dependentAssembly>
        <assemblyIdentity name="myAssembly" publicKeyToken="1524dba369a4decc"/>
        <bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0"/>

      </dependentAssembly>



    </assemblyBinding>
  </runtime>



</configuration>

Now C# file:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;

namespace MainProgram
{
    class Program
    {
        static void Main(string[] args)
        {

            //TODO! I WANT TO HAVE A PROBING PATH ON MY "findDLL", is it possible? HOW? :-)
            Assembly findDLL = Assembly.Load("myAssembly"); // use this not LoadFile, then you will need absolutepath

            Type customerType = findDLL.GetType("myAssembly.Customer");

            object cuInstance = Activator.CreateInstance(customerType);

            MethodInfo getFullNameMethod = customerType.GetMethod("getFullName");

            string[] param = new string[2];
            param[0] = "Khiem";
            param[1]= "Ho Xuan";


            //invvoke method

            string fname = (string)getFullNameMethod.Invoke(cuInstance, param);

            Console.WriteLine(fname);


        }
    }
}

//class library file for .dll

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace myAssembly
{
    public class Customer
    {
        public string getFullName(string first, string last)
        {
            return "Check " + first + " last " + last;
        }


    }
}

THANKS!

You can store the required information anywhere you want really. I use XML Configuration files because it's just a lot easier.

It looks a bit like this:

<OuterTag>
    <Assembly>Namespace.Library.Utility,Version=1.0,Culture=neutral</Assembly>
    <Type>Namespace.Library.Utility.Calculator</Type>
</OuterTag>

You can pass these arguments into the appropriate reflection libraries to load the required assembly.

NOTE: You cannot unload libraries once they are part of your current AppDomain. This means you can't switch between two different versions without reloading the app. It does mean that you can deploy different libraries to different clients though and the application won't care so long as the interfaces are correct. :)

commented: Deep knowledge +14
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.