1

I have a main application with a class "Table" which implements the interface "ITable" from the dll "Lib":

  • Main Application:

    using Lib;
    
    namespace One
    {
      public class Main
      {
        // Here the method "Work" from the plugin is called with first 
        // parameter a string and second parameter a dictionary with "Table"
        // objects and then it shows here the fault message
        plugin.Work("My Input", tableDictionary)
      }
    
      public class Table : ITable
      {
      }
    }
    
  • Class library: "Lib"

    namespace Two
    {
      public interface ITable
      {
      }
    }
    
  • Plugin-Project:

    using Lib;
    
    namespace Three
    {
      public class Example
      {
        public string Work(string input, Dictionary<string, ITable> tableDict)
        {
          // Here i want to use table objects from the main application
          // Do something with the table objects
        }
      }
    }
    

A plugin for the main application is created in a second project "Plugin-Project" which uses the dll (because i want to use objects/class "table" of the main application in this second "plugin"-project). I follow here this plugin-approach:

Creating a simple plugin mechanism

But when i use the table-object in the plugin project, import it then to my main application and run the main application, i get the error which says that it "Cannot convert from "Table" to "ITable"" It is not an exception while runtime. It is just a fault shown in the fault list of VisualStudio.

The problem is i want to use an Table-object within the plugin just like the way i can use it in the main application. Therefore i use the interface. But then i just can use an ITable object while my main application uses Table objects?


Update: I edited the problem description a bit, hope it is better understandable now. (Deleted also the ITable object test in the plugin, it was just not right). In "Main" of the main application the plugin is imported etc. (works fine and is not shown in the code above) and then the method "Work" from the plugin is called. My simplified main concern is: How can i use the table objects of the main application within the plugin?

DdarkSideE
  • 129
  • 1
  • 11
  • 1
    Please improve your sample code. First of all, rename the interface so that it uses an `I` prefix (`ITable`). And show us the exact code how and where you instantiate the objects and where exactly the exception occurs. – dymanoid Jul 19 '17 at 15:17
  • I really don't understand why do you try to create an instance of an interface. `ITable test = new ITable();` does not compile at all. "Cannot create an instace of the intarface" - compile error in visual studio. – DdarkSideE Jul 20 '17 at 07:11
  • "first parameter a string and second parameter a dictionary with "Table". Why don't you want to use dictionary with ITable as the second parametre? – DdarkSideE Jul 20 '17 at 07:46
  • This dictionary with table object is used in my whole application and i want to use it also in the plugin. The ITable interface should just be an helper (in the Table-class is a lot of further methods etc.) to get these table objects from the main application but i do not really know how to reach that... I do not want to write the whole implementation from the Table to the ITable class. –  Jul 20 '17 at 07:54
  • Oh. I undrestud what do you talk about. You want to use contravariance of a dictionary, but I think it is not possible. You can work around it. [This](https://stackoverflow.com/questions/12841458/idictionary-contravariance) might help you. – DdarkSideE Jul 20 '17 at 08:29

0 Answers0