How to pass a function to another class using IoC

berti

I am trying to implement inversion of control (IoC) in order to develop a plug-in based application, which needs to be able to pass data (e.g. strings) back to my main EXE when something happens. To achieve this, I am passing an Action to the DLL which it can use to send data back to the EXE. The following code shows a little test application to explain my beginner-problem:

namespace MainExe
{
    class Program
    {
        [Import(typeof(IDataProvider))]
        public IDataProvider stringProvider { get; set; }

        public void myCallback(string str)
        {
            Console.WriteLine(str);
        }

        public Program()
        {
            try
            {
                AggregateCatalog aggregatecatalogue = new AggregateCatalog();
                aggregatecatalogue.Catalogs.Add(new DirectoryCatalog(AppDomain.CurrentDomain.BaseDirectory));
                CompositionContainer container = new CompositionContainer(aggregatecatalogue);
                container.ComposeParts(this);    
            }
            catch (FileNotFoundException fnfex)
            {
                Console.WriteLine(fnfex.Message);
            }
            catch (CompositionException cex)
            {
                Console.WriteLine(cex.Message);
            }
        }

        void Run()
        {
            if (stringProvider != null)
            {
                stringProvider.SaySomething("myrequest", myCallback);
                Console.ReadKey();
            }
        }

        static void Main(string[] args)
        {
            Program program = new Program();
            program.Run();
        }
    }
}

My Interface looks like this:

namespace DataInterfaceDll
{
    public interface IDataProvider
    {
        void SaySomething(string request, Action<string> callback);
    }
}

And this is the Plug-In (DLL), which should be able to send something back to the EXE (or call a function from the EXE that does this):

namespace StringDataDll
{
    [Export(typeof(IDataProvider))]
    public class StringData : IDataProvider
    {
        public void SaySomething(string request, Action<string> callback)
        {
            callback("This is just a test program...");
        }
    }
}

When I run this code, I get a System.Reflection.ReflectionTypeLoadException in the following line

container.ComposeParts(this);

Can anyone tell me what I am doing wrong here?

Quentin Baradat

It works for me by loading dynamically the DLL.

aggregatecatalogue.Catalogs.Add(new AssemblyCatalog(Assembly.LoadFile("yourpath\StringDataDll.dll")));
aggregatecatalogue.Catalogs.Add(new AssemblyCatalog(Assembly.GetAssembly(typeof(IDataProvider))));

And it works too with new DirectoryCatalog(AppDomain.CurrentDomain.BaseDirectory) when you copy the stringDataDll.dll in the output folder of your main program.

Be sure to have all of your dll files in your output folder of your main program.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to pass two values from one function to another in the same class?

From Dev

C++ How to pass member function pointer to another class?

From Dev

How to pass values to another class without using startActivity()

From Dev

How do I pass a function from another class while calling std::async from a function of different class?

From Dev

How to pass inputs from one function to another using .apply method?

From Dev

How to pass JToggleButton to another class?

From Dev

How to pass entire Swift class instance to another class, while using a third party ViewController/class?

From Dev

C++ Pass a member function to another class

From Dev

How to pass parameters into constructors when using IoC containers?

From Dev

Using class/function in another module

From Dev

Using class/function in another module

From Dev

Using a function in another class in javascript

From Dev

how to pass the parameter from one function to another function but with in the same class in php

From Dev

How to pass a Class as an function parameter

From Dev

How to pass the function output to another function in powershell?

From Dev

How to pass array of function pointers to another function

From Dev

how to pass function output to another function

From Dev

How to pass a function as an parameter to another function

From Dev

How to pass a function to another function in python

From Dev

How to pass function as parameter to another function?

From Dev

How to pass a generic function as parameter to another function?

From Dev

How to pass a variable from a function to another function?

From Dev

How to call function in another class

From Dev

C++: How do I pass a pointer to a member function of another class?

From Dev

How can I pass a class method to another function like what happen in thread constructor

From Dev

How to pass data from callback function to MainPage(another class) in Windows Phone 8

From Dev

How can I pass a class method to another function like what happen in thread constructor

From Dev

Pass Dynamic Values from one function to another function in class

From Dev

cannot pass a variable from a function in a class to another function in Swift

Related Related

  1. 1

    How to pass two values from one function to another in the same class?

  2. 2

    C++ How to pass member function pointer to another class?

  3. 3

    How to pass values to another class without using startActivity()

  4. 4

    How do I pass a function from another class while calling std::async from a function of different class?

  5. 5

    How to pass inputs from one function to another using .apply method?

  6. 6

    How to pass JToggleButton to another class?

  7. 7

    How to pass entire Swift class instance to another class, while using a third party ViewController/class?

  8. 8

    C++ Pass a member function to another class

  9. 9

    How to pass parameters into constructors when using IoC containers?

  10. 10

    Using class/function in another module

  11. 11

    Using class/function in another module

  12. 12

    Using a function in another class in javascript

  13. 13

    how to pass the parameter from one function to another function but with in the same class in php

  14. 14

    How to pass a Class as an function parameter

  15. 15

    How to pass the function output to another function in powershell?

  16. 16

    How to pass array of function pointers to another function

  17. 17

    how to pass function output to another function

  18. 18

    How to pass a function as an parameter to another function

  19. 19

    How to pass a function to another function in python

  20. 20

    How to pass function as parameter to another function?

  21. 21

    How to pass a generic function as parameter to another function?

  22. 22

    How to pass a variable from a function to another function?

  23. 23

    How to call function in another class

  24. 24

    C++: How do I pass a pointer to a member function of another class?

  25. 25

    How can I pass a class method to another function like what happen in thread constructor

  26. 26

    How to pass data from callback function to MainPage(another class) in Windows Phone 8

  27. 27

    How can I pass a class method to another function like what happen in thread constructor

  28. 28

    Pass Dynamic Values from one function to another function in class

  29. 29

    cannot pass a variable from a function in a class to another function in Swift

HotTag

Archive