简体   繁体   中英

Call functions dynamics of DLL's inside a Web Service C#

I have a Web Service called 'A'.

On this WS I want only stay looking the installed folder, in other words, together main DLL file.

On this directory I plan to put "modules" of my project and share the functions (Operation Contracts) with my WS.

For example:

ModuleOne.dll have the function:

string[] getUsersFromDatabase();

MyService.dll have the function:

object CallEventByName(string eventName, params string parameters);

And I imagine the use:

foreach string file in fileList
// Check if DLL file have the function equal to 'eventName',
// call the function passing your parameters if have

When I create the requisition SOAP, indicate the function name and the parameters. My WS need the intelligence to manage this requisition and return the "return" haha.

Who I can to this?

Something like this , there may be a problem if dll which you want to load has dependencies , then you have load also this dll

    object CallFunction(string[] fileList , string eventName , object[] parameters){
        foreach(var file in fileList)
        {
            Assembly assem = Assembly.LoadFrom(file);
            foreach(var t in assem.GetTypes())
            {
                var methodInfo = t.GetMethod(eventName);
                if(methodInfo != null)
                {
                    var obj = Activator.CreateInstance(t);
                    return methodInfo.Invoke(obj , parameters);
                }    
            }
        }

        return null;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM