Casting a class/Interface using createInstance

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Shane

    Casting a class/Interface using createInstance

    I am having a problem where I create an instance of a class from an assembly
    and I try to cast it to an interface that it inherits from and it says that
    the cast is invalid.

    Here is the code:

    Interface.IPars er objPlugin;
    Interface.Plugi nServices parserService = new Interface.Plugi nServices();
    objPlugin = (Interface.IPar ser)parserServi ce.CreateInstan ce(_parser);
    return objPlugin.Parse rName;

    the CreateInstance above is a function that calls Assembly.LoadFr om and then
    subsequently createInstance. Everything in that function seems to operate
    normally returning an instance of my class that is contained in the assembly
    testParser.Test Parser which implements Interface.IPars er which is an
    interface class that I wrote.

    The problem is that the line
    "objPlugin = (Interface.IPar ser)parserServi ce.CreateInstan ce(_parser);"
    causes an invalid cast exception.




  • Mattias Sjögren

    #2
    Re: Casting a class/Interface using createInstance

    [color=blue]
    >the CreateInstance above is a function that calls Assembly.LoadFr om and then
    >subsequently createInstance.[/color]

    See http://www.gotdotnet.com/team/clr/Lo...Isolation.aspx



    Mattias

    --
    Mattias Sjögren [MVP] mattias @ mvps.org
    http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
    Please reply only to the newsgroup.

    Comment

    • Shane

      #3
      Re: Casting a class/Interface using createInstance

      Your right that article does seem to apply but then how come this works.

      Interface.Plugi nServices.Avail ablePlugin pars = new
      Interface.Plugi nServices.Avail ablePlugin();
      pars.AssemblyPa th = "C:\\Projects\\ testParser\\bin \\Debug\\testPa rser.dll";
      pars.ClassName = "testParser.Tes tParser";
      Interface.IPars er mytest = myP.CreateInsta nce(pars) as Interface.IPars er;

      where myP.CreateInsta nce is :
      Assembly objDLL;
      object objPlugin;
      //load dll
      objDLL = Assembly.LoadFr om(Plugin.Assem blyPath);
      //create and return class instance
      objPlugin = objDLL.CreateIn stance(Plugin.C lassName);
      return objPlugin;

      "Mattias Sjögren" wrote:
      [color=blue]
      >[color=green]
      > >the CreateInstance above is a function that calls Assembly.LoadFr om and then
      > >subsequently createInstance.[/color]
      >
      > See http://www.gotdotnet.com/team/clr/Lo...Isolation.aspx
      >
      >
      >
      > Mattias
      >
      > --
      > Mattias Sjögren [MVP] mattias @ mvps.org
      > http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
      > Please reply only to the newsgroup.
      >[/color]

      Comment

      • Shane

        #4
        Re: Casting a class/Interface using createInstance

        Sorry I didn't explain this better but Interface.IPars er is a statically
        referenced dll and you can see below that I am casting it to the reference
        created from my loadFrom which is a different dll but implements
        Interface.IPars er

        I guess I'm not sure what to do because my intention is to have my
        Interface.dll in several different projects so that I can ensure if I load
        one of my parsing dll's that they are implementing the correct Interface.

        Thanks for your help

        "Shane" wrote:
        [color=blue]
        > Your right that article does seem to apply but then how come this works.
        >
        > Interface.Plugi nServices.Avail ablePlugin pars = new
        > Interface.Plugi nServices.Avail ablePlugin();
        > pars.AssemblyPa th = "C:\\Projects\\ testParser\\bin \\Debug\\testPa rser.dll";
        > pars.ClassName = "testParser.Tes tParser";
        > Interface.IPars er mytest = myP.CreateInsta nce(pars) as Interface.IPars er;
        >
        > where myP.CreateInsta nce is :
        > Assembly objDLL;
        > object objPlugin;
        > //load dll
        > objDLL = Assembly.LoadFr om(Plugin.Assem blyPath);
        > //create and return class instance
        > objPlugin = objDLL.CreateIn stance(Plugin.C lassName);
        > return objPlugin;
        >
        > "Mattias Sjögren" wrote:
        >[color=green]
        > >[color=darkred]
        > > >the CreateInstance above is a function that calls Assembly.LoadFr om and then
        > > >subsequently createInstance.[/color]
        > >
        > > See http://www.gotdotnet.com/team/clr/Lo...Isolation.aspx
        > >
        > >
        > >
        > > Mattias
        > >
        > > --
        > > Mattias Sjögren [MVP] mattias @ mvps.org
        > > http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
        > > Please reply only to the newsgroup.
        > >[/color][/color]

        Comment

        • Shane

          #5
          Re: Casting a class/Interface using createInstance

          I haven't heard anything from my last post I'm still unclear as to what is
          going on in my code

          "Shane" wrote:
          [color=blue]
          > Sorry I didn't explain this better but Interface.IPars er is a statically
          > referenced dll and you can see below that I am casting it to the reference
          > created from my loadFrom which is a different dll but implements
          > Interface.IPars er
          >
          > I guess I'm not sure what to do because my intention is to have my
          > Interface.dll in several different projects so that I can ensure if I load
          > one of my parsing dll's that they are implementing the correct Interface.
          >
          > Thanks for your help
          >
          > "Shane" wrote:
          >[color=green]
          > > Your right that article does seem to apply but then how come this works.
          > >
          > > Interface.Plugi nServices.Avail ablePlugin pars = new
          > > Interface.Plugi nServices.Avail ablePlugin();
          > > pars.AssemblyPa th = "C:\\Projects\\ testParser\\bin \\Debug\\testPa rser.dll";
          > > pars.ClassName = "testParser.Tes tParser";
          > > Interface.IPars er mytest = myP.CreateInsta nce(pars) as Interface.IPars er;
          > >
          > > where myP.CreateInsta nce is :
          > > Assembly objDLL;
          > > object objPlugin;
          > > //load dll
          > > objDLL = Assembly.LoadFr om(Plugin.Assem blyPath);
          > > //create and return class instance
          > > objPlugin = objDLL.CreateIn stance(Plugin.C lassName);
          > > return objPlugin;
          > >
          > > "Mattias Sjögren" wrote:
          > >[color=darkred]
          > > >
          > > > >the CreateInstance above is a function that calls Assembly.LoadFr om and then
          > > > >subsequently createInstance.
          > > >
          > > > See http://www.gotdotnet.com/team/clr/Lo...Isolation.aspx
          > > >
          > > >
          > > >
          > > > Mattias
          > > >
          > > > --
          > > > Mattias Sjögren [MVP] mattias @ mvps.org
          > > > http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
          > > > Please reply only to the newsgroup.
          > > >[/color][/color][/color]

          Comment

          Working...