[C#, XNA] Problems with MethodInfo and string

This topic is 3433 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I Am Following Tutorials from "http://nickgravelyn.com/archive/#tileengine" to create a tile Engine, I am currently on Part 11 (A)/(b) and have come across some problems which he doesn't come across in the Video. I have reviewed the video again and again and cant see any code that I have missed. Here is the code that is causing me a problem with the error message
public class ConversationHandlerContent
{
public string Caption;
public MethodInfo Action;
public object[] ActionPara;
}
ConversationHandlerContent h = new ConversationHandlerContent();
string action = handlernode.Attributes["Action"].Value;

if(action.Contains(":"))
{
string[] actionSpilt = action.Split(':');
h.Action = actionSpilt[0]; //<-- Triggers Error 2
h.ActionPara = (object[])actionSpilt[1].Split(',');
}
else
{
h.Action = action; //<-- Triggers Error 3
h.ActionPara = null;
}

Error 2 Cannot implicitly convert type 'string' to 'System.Reflection.MethodInfo'  (filepath) 40 36 TileContent
Error 3 Cannot implicitly convert type 'string' to 'System.Reflection.MethodInfo'  (filepath) 45 36 TileContent


As I am New to MethodInfo, i dont quite understand what is going on here. In the Tutorial Video the code is Extaclty the same as this, However it didnt trigger the Error. i just odnt understand why mine should trigger this error any help is appreciated :) Thanks in Advance

Share on other sites
After looking i can see that :

MethodInfo action;//I cannot call anything by referencing action as followsaction.something

so maybe I am trying to set a data member of MethodInfo that doesnt exist by doing

h.action = action;

Share on other sites
I'm pretty sure it should read something like:

h.Action = MethodInfo.GetMethod(action)

Share on other sites
h.action = MethodInfo.GetMethod(action);

No such method
Error	3	'System.Reflection.MethodInfo' does not contain a definition for 'GetMethod'	(filepath)	45	47	TileContent

Share on other sites
Sorry my bad, it's a non-static member of the Type class.

GetMethod()

Share on other sites
So far i have got rid of the errors by Doing the following

h.action = typeof(ConversationHandlerContent).GetMethod(actionSpilt[0]);h.action = typeof(ConversationHandlerContent).GetMethod(action);

seemed to have cleaned up my errors

Share on other sites
That will solve your compilation errors, but you'll get runtime errors instead. The ConversationHandlerContent class has no methods at all, so regardless of the names of the actions, the GetMethod class will always return null.

The type you use for the GetMethod call needs to be the type that contains the functions that you're trying to call (function names that match the 'action' string).

I'm guessing they're static methods of some 'action' class or something, so your syntax should be something like that used in the example code shown on the msdn link. But without knowing more about the code structure I can't give you a specific answer.

1. 1
2. 2
Rutin
17
3. 3
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• Forum Statistics

• Total Topics
633735
• Total Posts
3013595
×