Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Interface Type-casting problem

Interface Type-casting problem

Scheduled Pinned Locked Moved C#
helpdebugging
3 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hhrafn
    wrote on last edited by
    #1

    Hi, I'm trying to typecast a class that I load from a dll. This class is using a certain interface and I'd like to type-cast it into the same interface (see code). I should also point out that the main class and the class that I'm trying to load into it are in two seperate projects (and namespaces). But my problem is that when I run this I get the error: "Unable to cast object 'Class1' to type 'IPlugin'." :confused: using System; using System.Collections.Generic; using System.Text; class Class1 : IPlugin { public Class1() { } public String getInfo() { return "weeee"; } } public interface IPlugin { String getInfo(); } namespace DLL_LoadTest { class Program { static void Main(string[] args) { Assembly assembly = Assembly.LoadFrom(@"C:\Test\ClassLibrary1\bin\Debug\ClassLibrary1.dll"); IPlugin plugin = null; plugin = (IPlugin)assembly.CreateInstance("Class1"); } } } please help! thanks, HHG -- modified at 10:21 Sunday 26th March, 2006

    G 1 Reply Last reply
    0
    • H hhrafn

      Hi, I'm trying to typecast a class that I load from a dll. This class is using a certain interface and I'd like to type-cast it into the same interface (see code). I should also point out that the main class and the class that I'm trying to load into it are in two seperate projects (and namespaces). But my problem is that when I run this I get the error: "Unable to cast object 'Class1' to type 'IPlugin'." :confused: using System; using System.Collections.Generic; using System.Text; class Class1 : IPlugin { public Class1() { } public String getInfo() { return "weeee"; } } public interface IPlugin { String getInfo(); } namespace DLL_LoadTest { class Program { static void Main(string[] args) { Assembly assembly = Assembly.LoadFrom(@"C:\Test\ClassLibrary1\bin\Debug\ClassLibrary1.dll"); IPlugin plugin = null; plugin = (IPlugin)assembly.CreateInstance("Class1"); } } } please help! thanks, HHG -- modified at 10:21 Sunday 26th March, 2006

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      Do you cast it into the actual interface that the class uses, or have you just created an interface with the same name in your code? --- b { font-weight: normal; }

      H 1 Reply Last reply
      0
      • G Guffa

        Do you cast it into the actual interface that the class uses, or have you just created an interface with the same name in your code? --- b { font-weight: normal; }

        H Offline
        H Offline
        hhrafn
        wrote on last edited by
        #3

        Uhm.. yes .. well.. yeah, I was using two interfaces (one in each project) both with the same name. X| Once I referenced to the other one everything started to work :cool: Thanks for the help, I just love it when the solution is too simple :P

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups