Note that there are some explanatory texts on larger screens.

plurals
  1. POInterface and Implementation in Separate Assemblies Using Reflection
    primarykey
    data
    text
    <p>I have two assemblies; <code>AssemblyWithInterface</code> and <code>AssemblyWithClass</code>. In <code>AssemblyWithInterface</code> I have an interface called <code>IDoSomething</code> which is implemented by <code>TheClass</code> in <code>AssemblyWithClass</code>. (<code>AssemblyWithClass</code> references <code>AssemblyWithInterface</code>.)</p> <p>In <code>AssemblyWithInterface</code> I want to create instances of the class from <code>AssemblyWithClass</code> using reflection:</p> <pre><code>var theAssembly = Assembly.Load("Company.AssemblyWithClass, { FQN... }"); var theConcreteClass = theAssembly.CreateInstance("Company.AssemblyWithClass.TheClass"); </code></pre> <p>The assembly loads fine and the instance is created as a <code>TheConcreteClass</code>.</p> <p>However, I'm unable to cast <code>theConcreteClass</code> to its implementing interface. I get an <code>InvalidCastException</code> here:</p> <pre><code>var theConcreteClassInterfaced = (IDoSomething)theConcreteClass; </code></pre> <p>And</p> <pre><code>var isAssignable = typeof(IDoSomething).IsAssignableFrom(theConcreteClass.GetType(); </code></pre> <p>is False.</p> <p>What am I missing? (The goal is to have a command pattern-styled ability to add commands implementing the <code>IDoSomething</code> to the <code>AssemblyWithClass</code> and be able to execute them in <code>AssemblyWithInterface</code> without changing code in the <code>AssemblyWithInterface</code>.)</p> <p>Platform is .NET 3.5 (can't use dynamic).</p> <p><strong>Update:</strong> The background for this question (to explain why I'm not complying to DIP) is that I have a legacy ASP.NET-application contained in one large assembly. I want to create a plugin assembly which can call into various parts of the legacy assembly to perform montoring and some automated tasks. I don't want any additional dependencies (references to other assemblies) added to the legacy assembly. The idea is to implement a hook in the legacy assembly (a new special page and a IPlugInOperation), add a montoring page with a corresponding code behind. Have the code behind execute various IPlugInOperations (drawing an interface to allow the admin to specify parameters to use for executing code in the legacy assembly). The PlugIn Assembly must reference the legacy assembly and the legacy assembly uses reflection to list and allow the admin to excute the various implementations of IPlugInOperation contained in the PlugIn Assembly.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload