Palani
Palani

Reputation: 9062

.Net Load Reference at runtime

I have following requirement,

Upvotes: 4

Views: 5188

Answers (8)

Martin Harris
Martin Harris

Reputation: 28647

You can add a probing element to your application's config file. This allows other folders than the default to be checked for required libraries. See here.

It still must be below the exe folder though for security reasons.

Upvotes: 4

William Edmondson
William Edmondson

Reputation: 3635

You will need to load your dll and methods using reflection (using System.Reflection namespace) using a combination of Assembly.LoadFrom and Type.GetType and Type.GetMethod.

Here is an example on how to use reflection to dynamically call a method from a dll loaded at runtime.

static string DynamicMethodCall(string dllPath, string someClass, string someMethodName, object[] parameters)
        {
            Assembly dll = Assembly.LoadFile(dllPath);

            Type type = dll.GetType(someClass);

            if (type == null)
            {
                Exception ex = new Exception("class not found");
                throw ex;
            }

            MethodInfo method = type.GetMethod(someMethodName);

            if (method == null)
            {
                Exception ex = new Exception("method not found");
                throw ex;
            }

            if (parameters.Length >= 1)
            {
                object[] myparam = new object[1];
                myparam[0] = parameters;
                return (string)method.Invoke(null, myparam);
            }
            else
            {
                return (string)method.Invoke(null, null);
            }
        }

Upvotes: 1

Sam Holder
Sam Holder

Reputation: 32954

you can use manual assembly resolution to do this.

You need to provide a delegate to the AssemblyResolve event in the current AppDomain

AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.AssemblyResolve += assemblyResolver.ResolveEventHandler;

when the application has any assembly references that it can't resolve it will call this delegate to get the assembly resolved. You can then simply return the assembly requested from the delegate:

Assembly assembly = Assembly.LoadFrom (assemblyPath);
return assembly;

hope this helps

Upvotes: 10

Ricardo
Ricardo

Reputation: 1023

You can use reflection: http://msdn.microsoft.com/en-us/library/system.reflection.assembly.load%28VS.71%29.aspx

Be careful because of performance. Reflection must be used carefully.

Upvotes: 0

Fredrik Mörk
Fredrik Mörk

Reputation: 158409

I can't see the reason for having referenced assemblies apart from the exe file, but one way to do it is to place them in a separate directory under the directory in which the exe file is located and include a probing element in the app.config file.

Upvotes: 0

Philippe Leybaert
Philippe Leybaert

Reputation: 171914

You could use Assembly.LoadFrom(filename)

But as commenters pointed out, I don't see why you would want to do that?

Upvotes: 1

MusiGenesis
MusiGenesis

Reputation: 75396

You could include the DLL in your EXE project as an embedded resource, and when your application starts you could save the DLL out to your EXE file directory (and delete it when the application ends, although I don't know what the problem is with having the DLL in the EXE directory). This will work as long as you perform the extraction before loading anything that references SqlLite.

Update: since you want to do plugin-style DLLs, you can keep the DLLs in a separate directory, and copy them into the EXE folder on application startup.

Upvotes: 0

abhilash
abhilash

Reputation: 5651

I'm not sure why you want to do this again, it's a deviation from the best practices, But to answer your question: You could use

Assembly.LoadFrom Method (String path)

From the System.Reflection Namespace

& having the path in the app.config file

Assembly.LoadFrom on MSDN

Upvotes: 4

Related Questions