"Object does not match target type." using dynamic compiling & inv

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

    #1

    "Object does not match target type." using dynamic compiling & inv

    Howdy!

    I posted this question on CSharpCorner.co m, but then realized I should
    probably post it on a more active newsgroup. This will be my only cross-post.

    I'm creating a game engine, and using CodeDOM for my scripting needs (I
    realize I could use yacc or something else, but I wanted to try using CodeDOM
    -- this is more of an exercise for me to learn this stuff).

    Well, I compile my in-game scripts just fine. See appendix A for my code
    that does that (it was too cluttered to paste right here. Suffice to say, it
    works)

    I'm referencing my own executable, so that I can share code/types from my
    main application.

    Now, I go over and try to invoke the method from the assembly I just
    compiled. The code that tries that looks like this:

    object retObj = null;
    object[] scriptParams = new object[1];
    scriptParams[0] = context; // "context" is of type miteScriptConte xt, which
    is what the method is expecting
    retObj = script.ScriptMe thod.Invoke(scr ipt.asm, scriptParams);


    However, it throws the following error for me:

    "Unhandled Exception: System.Reflecti on.TargetExcept ion: Object does not
    match target type."

    So I'm thinking, "okay, I must be having a wrong parameter", but I checked
    and double-checked and my code seems correct. *FINALLY* I wrote the following
    debug code:

    object[] scriptParams = new object[1];
    scriptParams[0] = context;

    Console.WriteLi ne("Method return type is: " +
    script.ScriptMe thod.ReturnType .ToString());
    Console.WriteLi ne("Passing in...");
    Console.WriteLi ne(scriptParams[0].GetType().ToSt ring() + ": " +
    scriptParams[0].GetHashCode(). ToString());
    Console.WriteLi ne(scriptParams[0].GetType().Asse mbly.Location);
    Console.WriteLi ne(scriptParams[0].GetType().Asse mbly.FullName);
    Console.WriteLi ne();
    Console.WriteLi ne("...into..." );
    Console.WriteLi ne(script.Scrip tMethod.GetPara meters()[0].ParameterType. ToString()
    + ": " +
    script.ScriptMe thod.GetParamet ers()[0].ParameterType. GetHashCode().T oString())
    Console.WriteLi ne(script.Scrip tMethod.GetPara meters()[0].ParameterType. Assembly.Locati on)
    Console.WriteLi ne(script.Scrip tMethod.GetPara meters()[0].ParameterType. Assembly.FullNa me);
    Console.WriteLi ne();

    retObj = script.ScriptMe thod.Invoke(scr ipt.asm, scriptParams);

    That outputs the following...


    Method return type is: System.Object
    Passing in...
    MITE.GameData.S cripting.miteSc riptContext: 4
    C:\Documents and Settings\cherro n\My Documents\Sharp Develop
    Projects\MITE\b in\Debug\MITE.e xe
    MITE, Version=1.0.198 6.18485, Culture=neutral , PublicKeyToken= null

    ....into...
    MITE.GameData.S cripting.miteSc riptContext: 202521200
    C:\Documents and Settings\cherro n\My Documents\Sharp Develop
    Projects\MITE\b in\Debug\MITE.e xe
    MITE, Version=1.0.198 6.18485, Culture=neutral , PublicKeyToken= null


    Okay. That's *REALLY* confusing to me. Basically it's saying that the types
    don't match up. I'm passing in an object of type miteScriptConte xt. Both
    types say that they come from the same assembly, from the same version,
    everything. However, when referenced in different ways, that type comes up
    with different hash codes?

    Am I on crack, or is something wrong here? How can it be getting the same
    type from the same assembly in two different ways and thinking that they're
    different?

    If anyone can point me in the right direction, that would be wonderful.

    I know this method of calling scripts works, because I have another project
    where I'm practicing the same method and it works just fine! I can't figure
    out what's different about *this* project that makes it generate different
    hash codes for the same type (which effectively makes Reflection think that
    they're different types altogether). I would like to post a small sample app,
    and if I need to create one, I can, it's just that there's a lot of overhead
    involved, and it wouldn't be a very "small" app. :) I was hoping someone
    would understand the theory of the way .NET determines if types are
    compatible, and could help me out from a theory point of view.

    Help, please? :)

    Thanks!

    Respectfully,
    clint


    Appendix A: My code to compile an assembly from code at runtime

    public static Assembly CompileCode(str ing code)
    {
    Microsoft.CShar p.CSharpCodePro vider provider = new
    Microsoft.CShar p.CSharpCodePro vider();
    System.CodeDom. Compiler.ICodeC ompiler compiler = provider.Create Compiler();
    System.CodeDom. Compiler.Compil erParameters param = new
    System.CodeDom. Compiler.Compil erParameters();
    System.CodeDom. Compiler.Compil erResults results;

    param.GenerateI nMemory = true;
    param.GenerateE xecutable = false;
    param.Reference dAssemblies.Add (Assembly.GetEx ecutingAssembly ().Location);

    results = compiler.Compil eAssemblyFromSo urce(param, code);

    if (results.Errors .HasErrors)
    {
    string errText = "";
    foreach (System.CodeDom .Compiler.Compi lerError err in results.Errors)
    {
    errText += ("Line [" + err.Line.ToStri ng() + "] :" + err.ErrorText + "\r\n");
    }
    // TODO: Better error reporting mechanism.
    System.Windows. Forms.MessageBo x.Show(errText,
    results.Errors. Count.ToString( ) + " errors");
    return null;
    }

    return (results.Compil edAssembly);
    }
  • Jon Skeet [C# MVP]

    #2
    Re: "Object does not match target type." using dynamic compiling & inv

    Clint Herron <ClintHerron@di scussions.micro soft.com> wrote:[color=blue]
    > I posted this question on CSharpCorner.co m, but then realized I should
    > probably post it on a more active newsgroup. This will be my only cross-post.
    >
    > I'm creating a game engine, and using CodeDOM for my scripting needs (I
    > realize I could use yacc or something else, but I wanted to try using CodeDOM
    > -- this is more of an exercise for me to learn this stuff).
    >
    > Well, I compile my in-game scripts just fine. See appendix A for my code
    > that does that (it was too cluttered to paste right here. Suffice to say, it
    > works)
    >
    > I'm referencing my own executable, so that I can share code/types from my
    > main application.[/color]

    <snip>

    Check whether
    scriptParams[0].GetType()==
    script.ScriptMe thod.GetParamet ers(0).Paramete rType

    I suspect they won't, even though they're loaded from the same assembly
    file. My guess is that you've got two copies of the same assembly in
    memory.

    Could you post a short but complete program which demonstrates the
    problem?

    See http://www.pobox.com/~skeet/csharp/complete.html for details of
    what I mean by that. I know it may be hard, given the other things you
    said, but if you can do it, it would be very helpful.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Clint Herron

      #3
      Re: &quot;Object does not match target type.&quot; using dynamic compiling &amp;

      Hey Jon! Thanks for your great replies (in this, and in other threads).
      [color=blue]
      > Check whether
      > scriptParams[0].GetType()==
      > script.ScriptMe thod.GetParamet ers(0).Paramete rType[/color]

      Oh interesting!It shows up as "true".

      That was surprising to me -- I would have assumed "false" given everything
      else that I'm seeing.

      So perhaps it's another object that isn't matching type? Is there any way of
      telling what object it's messing up on? It's a horrible error message. I'm
      not afraid of digging into the CIL if I'm told what to look for.

      Thanks!

      Respectfully,
      clint

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: &quot;Object does not match target type.&quot; using dynamic compiling &amp;

        Clint Herron <ClintHerron@di scussions.micro soft.com> wrote:[color=blue]
        > Hey Jon! Thanks for your great replies (in this, and in other threads).
        >[color=green]
        > > Check whether
        > > scriptParams[0].GetType()==
        > > script.ScriptMe thod.GetParamet ers(0).Paramete rType[/color]
        >
        > Oh interesting!It shows up as "true".
        >
        > That was surprising to me -- I would have assumed "false" given everything
        > else that I'm seeing.[/color]

        Likewise.
        [color=blue]
        > So perhaps it's another object that isn't matching type? Is there any way of
        > telling what object it's messing up on? It's a horrible error message. I'm
        > not afraid of digging into the CIL if I'm told what to look for.[/color]

        Have you checked that the target of the call (i.e. the object you're
        calling the method *on*) is of the right type?

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        • Clint Herron

          #5
          Re: &quot;Object does not match target type.&quot; using dynamic compiling &amp;

          Oh my goodness, I'm so schtupid.

          I was passing in the assembly, rather than an instance of the class inside
          the assembly.

          Wow, thankyou. You were a huge help!!!

          Here is output, showing the compiled "get" script in action:
          [color=blue]
          >[/color]
          get flask
          You try to pick up the flask, but fail. A strange voice booms. 'That feature
          is
          not yet implemented.'


          Thanks again Jon! Is there anything I can do to vote for you or boost your
          rating in any way? You've been incredibly helpful. Thanks!

          --clint

          "Jon Skeet [C# MVP]" wrote:
          [color=blue]
          > Clint Herron <ClintHerron@di scussions.micro soft.com> wrote:[color=green]
          > > Hey Jon! Thanks for your great replies (in this, and in other threads).
          > >[color=darkred]
          > > > Check whether
          > > > scriptParams[0].GetType()==
          > > > script.ScriptMe thod.GetParamet ers(0).Paramete rType[/color]
          > >
          > > Oh interesting!It shows up as "true".
          > >
          > > That was surprising to me -- I would have assumed "false" given everything
          > > else that I'm seeing.[/color]
          >
          > Likewise.
          >[color=green]
          > > So perhaps it's another object that isn't matching type? Is there any way of
          > > telling what object it's messing up on? It's a horrible error message. I'm
          > > not afraid of digging into the CIL if I'm told what to look for.[/color]
          >
          > Have you checked that the target of the call (i.e. the object you're
          > calling the method *on*) is of the right type?
          >
          > --
          > Jon Skeet - <skeet@pobox.co m>
          > http://www.pobox.com/~skeet
          > If replying to the group, please do not mail me too
          >[/color]

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: &quot;Object does not match target type.&quot; using dynamic compiling &amp;

            Clint Herron <ClintHerron@di scussions.micro soft.com> wrote:[color=blue]
            > Oh my goodness, I'm so schtupid.
            >
            > I was passing in the assembly, rather than an instance of the class inside
            > the assembly.[/color]

            Goodo :)
            [color=blue]
            > Wow, thankyou. You were a huge help!!!
            >
            > Here is output, showing the compiled "get" script in action:
            >
            > get flask
            > You try to pick up the flask, but fail. A strange voice booms. 'That feature
            > is
            > not yet implemented.'[/color]

            LOL :) That takes me back to the days when I was porting Colossal Cave
            to run on WAP phones.
            [color=blue]
            > Thanks again Jon! Is there anything I can do to vote for you or boost your
            > rating in any way? You've been incredibly helpful. Thanks![/color]

            Just your thanks are more than enough :)

            --
            Jon Skeet - <skeet@pobox.co m>
            Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

            If replying to the group, please do not mail me too

            Comment

            • Clint Herron

              #7
              Re: &quot;Object does not match target type.&quot; using dynamic compiling &amp;

              Oh my goodness, I'm so schtupid.

              I was passing in the assembly, rather than an instance of the class inside
              the assembly.

              Wow, thankyou. You were a huge help!!!

              Here is output, showing the compiled "get" script in action:
              [color=blue]
              >[/color]
              get flask
              You try to pick up the flask, but fail. A strange voice booms. 'That feature
              is
              not yet implemented.'


              Thanks again Jon! Is there anything I can do to vote for you or boost your
              rating in any way? You've been incredibly helpful. Thanks!

              --clint

              "Jon Skeet [C# MVP]" wrote:
              [color=blue]
              > Clint Herron <ClintHerron@di scussions.micro soft.com> wrote:[color=green]
              > > Hey Jon! Thanks for your great replies (in this, and in other threads).
              > >[color=darkred]
              > > > Check whether
              > > > scriptParams[0].GetType()==
              > > > script.ScriptMe thod.GetParamet ers(0).Paramete rType[/color]
              > >
              > > Oh interesting!It shows up as "true".
              > >
              > > That was surprising to me -- I would have assumed "false" given everything
              > > else that I'm seeing.[/color]
              >
              > Likewise.
              >[color=green]
              > > So perhaps it's another object that isn't matching type? Is there any way of
              > > telling what object it's messing up on? It's a horrible error message. I'm
              > > not afraid of digging into the CIL if I'm told what to look for.[/color]
              >
              > Have you checked that the target of the call (i.e. the object you're
              > calling the method *on*) is of the right type?
              >
              > --
              > Jon Skeet - <skeet@pobox.co m>
              > http://www.pobox.com/~skeet
              > If replying to the group, please do not mail me too
              >[/color]

              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: &quot;Object does not match target type.&quot; using dynamic compiling &amp;

                Clint Herron <ClintHerron@di scussions.micro soft.com> wrote:[color=blue]
                > Oh my goodness, I'm so schtupid.
                >
                > I was passing in the assembly, rather than an instance of the class inside
                > the assembly.[/color]

                Goodo :)
                [color=blue]
                > Wow, thankyou. You were a huge help!!!
                >
                > Here is output, showing the compiled "get" script in action:
                >
                > get flask
                > You try to pick up the flask, but fail. A strange voice booms. 'That feature
                > is
                > not yet implemented.'[/color]

                LOL :) That takes me back to the days when I was porting Colossal Cave
                to run on WAP phones.
                [color=blue]
                > Thanks again Jon! Is there anything I can do to vote for you or boost your
                > rating in any way? You've been incredibly helpful. Thanks![/color]

                Just your thanks are more than enough :)

                --
                Jon Skeet - <skeet@pobox.co m>
                Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                If replying to the group, please do not mail me too

                Comment

                Working...