Sign in to follow this  
Andy474

[C#, XNA] Problems with MethodInfo and string

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;
}

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

Share this post


Link to post
Share on other sites
After looking i can see that :


MethodInfo action;

//I cannot call anything by referencing action as follows
action.something


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


h.action = action;

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this