473,573 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"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);
}
Nov 17 '05 #1
7 9976
Clint Herron <Cl*********@di scussions.micro soft.com> wrote:
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.


<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 - <sk***@pobox.co m>
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.ScriptMe thod.GetParamet ers(0).Paramete rType


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*********@di scussions.micro soft.com> wrote:
Hey Jon! Thanks for your great replies (in this, and in other threads).
Check whether
scriptParams[0].GetType()==
script.ScriptMe thod.GetParamet ers(0).Paramete rType
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.co m>
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*********@di scussions.micro soft.com> wrote:
Hey Jon! Thanks for your great replies (in this, and in other threads).
Check whether
scriptParams[0].GetType()==
script.ScriptMe thod.GetParamet ers(0).Paramete rType


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

Nov 17 '05 #5
Clint Herron <Cl*********@di scussions.micro soft.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.co m>
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*********@di scussions.micro soft.com> wrote:
Hey Jon! Thanks for your great replies (in this, and in other threads).
Check whether
scriptParams[0].GetType()==
script.ScriptMe thod.GetParamet ers(0).Paramete rType


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

Nov 17 '05 #7
Clint Herron <Cl*********@di scussions.micro soft.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.co m>
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
1460
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 table called "tblCars" that has the fields fYear (integer), fModel (string), fMake (string). When I perform a query in AB, I do something like: ...
0
1373
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 to format the designer output. Anyone know whats going on? I don't get any real errors...
35
3187
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 instance of an object" for the line 'If mpg.FindControl("lkred").Visible = True Then'. I couldn't find sofar the solution. Any help would be...
3
2471
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 have the function something like (w/ Prototype)
1
7090
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 someone who bothers to read all of it have some pointers. Note, I have posted the stack trace and the code exhibiting the problem further down...
14
2505
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 = MyClass() a.b = 1 # OK
2
3960
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 of DLL. i.e Method of file A -Method of file B -Method of DLL. But in the file B's method while calling DLL's method it is giving me Error...
4
2429
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
4291
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 file. The portion that is not working is the star rating part <?php session_start(); include_once('includes/host_conf.php');...
0
7788
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7707
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8037
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8213
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
5604
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3742
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2226
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1320
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1048
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.