473,416 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

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

Howdy!

I posted this question on CSharpCorner.com, 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 miteScriptContext, which
is what the method is expecting
retObj = script.ScriptMethod.Invoke(script.asm, scriptParams);
However, it throws the following error for me:

"Unhandled Exception: System.Reflection.TargetException: 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.WriteLine("Method return type is: " +
script.ScriptMethod.ReturnType.ToString());
Console.WriteLine("Passing in...");
Console.WriteLine(scriptParams[0].GetType().ToString() + ": " +
scriptParams[0].GetHashCode().ToString());
Console.WriteLine(scriptParams[0].GetType().Assembly.Location);
Console.WriteLine(scriptParams[0].GetType().Assembly.FullName);
Console.WriteLine();
Console.WriteLine("...into...");
Console.WriteLine(script.ScriptMethod.GetParameter s()[0].ParameterType.ToString()
+ ": " +
script.ScriptMethod.GetParameters()[0].ParameterType.GetHashCode().ToString())
Console.WriteLine(script.ScriptMethod.GetParameter s()[0].ParameterType.Assembly.Location)
Console.WriteLine(script.ScriptMethod.GetParameter s()[0].ParameterType.Assembly.FullName);
Console.WriteLine();

retObj = script.ScriptMethod.Invoke(script.asm, scriptParams);

That outputs the following...
Method return type is: System.Object
Passing in...
MITE.GameData.Scripting.miteScriptContext: 4
C:\Documents and Settings\cherron\My Documents\SharpDevelop
Projects\MITE\bin\Debug\MITE.exe
MITE, Version=1.0.1986.18485, Culture=neutral, PublicKeyToken=null

....into...
MITE.GameData.Scripting.miteScriptContext: 202521200
C:\Documents and Settings\cherron\My Documents\SharpDevelop
Projects\MITE\bin\Debug\MITE.exe
MITE, Version=1.0.1986.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 miteScriptContext. 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(string code)
{
Microsoft.CSharp.CSharpCodeProvider provider = new
Microsoft.CSharp.CSharpCodeProvider();
System.CodeDom.Compiler.ICodeCompiler compiler = provider.CreateCompiler();
System.CodeDom.Compiler.CompilerParameters param = new
System.CodeDom.Compiler.CompilerParameters();
System.CodeDom.Compiler.CompilerResults results;

param.GenerateInMemory = true;
param.GenerateExecutable = false;
param.ReferencedAssemblies.Add(Assembly.GetExecuti ngAssembly().Location);

results = compiler.CompileAssemblyFromSource(param, code);

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

return (results.CompiledAssembly);
}
Nov 17 '05 #1
7 9958
Clint Herron <Cl*********@discussions.microsoft.com> wrote:
I posted this question on CSharpCorner.com, 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.


<snip>

Check whether
scriptParams[0].GetType()==
script.ScriptMethod.GetParameters(0).ParameterType

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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
Hey Jon! Thanks for your great replies (in this, and in other threads).
Check whether
scriptParams[0].GetType()==
script.ScriptMethod.GetParameters(0).ParameterType


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
Nov 17 '05 #3
Clint Herron <Cl*********@discussions.microsoft.com> wrote:
Hey Jon! Thanks for your great replies (in this, and in other threads).
Check whether
scriptParams[0].GetType()==
script.ScriptMethod.GetParameters(0).ParameterType
Oh interesting!It shows up as "true".

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


Likewise.
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.


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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4
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:
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:
Clint Herron <Cl*********@discussions.microsoft.com> wrote:
Hey Jon! Thanks for your great replies (in this, and in other threads).
Check whether
scriptParams[0].GetType()==
script.ScriptMethod.GetParameters(0).ParameterType


Oh interesting!It shows up as "true".

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


Likewise.
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.


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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #5
Clint Herron <Cl*********@discussions.microsoft.com> wrote:
Oh my goodness, I'm so schtupid.

I was passing in the assembly, rather than an instance of the class inside
the assembly.
Goodo :)
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.'
LOL :) That takes me back to the days when I was porting Colossal Cave
to run on WAP phones.
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!


Just your thanks are more than enough :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #6
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:
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:
Clint Herron <Cl*********@discussions.microsoft.com> wrote:
Hey Jon! Thanks for your great replies (in this, and in other threads).
Check whether
scriptParams[0].GetType()==
script.ScriptMethod.GetParameters(0).ParameterType


Oh interesting!It shows up as "true".

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


Likewise.
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.


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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #7
Clint Herron <Cl*********@discussions.microsoft.com> wrote:
Oh my goodness, I'm so schtupid.

I was passing in the assembly, rather than an instance of the class inside
the assembly.
Goodo :)
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.'
LOL :) That takes me back to the days when I was porting Colossal Cave
to run on WAP phones.
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!


Just your thanks are more than enough :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Randy Yates | last post by:
Having done a bit of Access Basic programming, I'm realizing that AB does seem to have (as much as I hate to admit it since I think it's a toy language) an advantage over C++. Let's say I have a...
0
by: schneider | last post by:
Collection editor problems. I keep getting "Object does not match target type." I think it has something to do with the TypeConverter, because it work fine with-out it. I need the converter...
35
by: Chris | last post by:
Hi, I tried to create a class which must change the propety 'visible' of a <linktag in the masterpage into 'false' when the user is logged. But i get the error: "Object reference not set to an...
3
by: Good Man | last post by:
Hi there Ideally, I'd like to create one javascript function and pass the file extension i'm looking for to see if its there: <input type="file" onchange="checkFile('pdf',this)" /> and then...
1
by: =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?= | last post by:
I get the above error in some of the ASP.NET web applications on a server, and I need some help figuring out how to deal with it. This is a rather long post, and I hope I have enough details that...
14
by: =?GB2312?B?zPC5zw==?= | last post by:
Howdy, I wonder why below does not work. a = object() a.b = 1 # dynamic bind attribute failed... To make it correct, we have to create a new class: class MyClass(object): pass a =...
2
by: =?Utf-8?B?U3dhcHB5?= | last post by:
hi, I am working on application in this i am using two files. In first (consider A) file i am calling the function of other file (consider B). In that function of file B i am calling the method...
4
by: =?Utf-8?B?U3dhcHB5?= | last post by:
hi, Im running this code public Boolean IsLayoutOpen(String strLayoutName) { Layout Layout_Obj = null; try { Layouts layouts = instrumentation.Layouts; //This call is through COM object
2
by: bips2008 | last post by:
The code seems to work fine in other browser but in IE it throws this error. This is very urgent for me and any help would be greatly appreciated For your convienence i have posted the code for the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.