[C#, XNA] Problems with MethodInfo and string

Started by
5 comments, last by Sandman 14 years, 9 months ago
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;
}

c.Handlers.Add(h);

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
Advertisement
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;
I'm pretty sure it should read something like:

h.Action = MethodInfo.GetMethod(action)

h.action = MethodInfo.GetMethod(action); 


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

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

GetMethod()
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
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.

This topic is closed to new replies.

Advertisement