473,765 Members | 2,010 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 9999
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
1465
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: Dim rstCars As Recordset Dim year As Integer
0
1385
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
3231
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 appreciated ... Thanks
3
2478
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
7112
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 so if you want to start by reading that, search for +++ Also note that I am unable to reproduce...
14
2519
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
3975
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 "Object does not match target type.".
4
2445
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
4307
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'); include_once('includes/db_connect.php'); include_once('includes/mysql.lib.php');...
0
9404
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10007
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8833
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5277
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3926
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
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.