[C#] Methodinfo Invoke

Junior Spellweaver
Joined
Mar 25, 2007
Messages
126
Reaction score
0
Ok, i have a server and it will process the data that it recieves, and instead of using a switch statement:

PHP:
public void Process(string DataArrived)
{
switch(DataArrived)
{
case "1":
---
break;
case "2":
---
break;
case "3":
---
break;
}
}
I am trying to invoke methods (i read it was alot faster):

PHP:
public void Process(string DataArrived)
{
Functions Function = new Functions(this);
            try
            {
                    //The method to execute
                    MethodInfo Method = typeof(Functions).GetMethod(theData);

                    Method.Invoke(Function, null);
                    Write.Normal(Method.ToString());
            }
            catch
            {
            }
}
PHP:
 Functions Function = new Functions(this)    is the Functions class btw
But the problem is that it only executes the first piece of data that arrives and then it wont invoke the new data.

Can anyone help me please?
 
Ok, i have a server and it will process the data that it recieves, and instead of using a switch statement:

PHP:
public void Process(string DataArrived)
{
switch(DataArrived)
{
case "1":
---
break;
case "2":
---
break;
case "3":
---
break;
}
}
I am trying to invoke methods (i read it was alot faster):

PHP:
public void Process(string DataArrived)
{
Functions Function = new Functions(this);
            try
            {
                    //The method to execute
                    MethodInfo Method = typeof(Functions).GetMethod(theData);

                    Method.Invoke(Function, null);
                    Write.Normal(Method.ToString());
            }
            catch
            {
            }
}
PHP:
 Functions Function = new Functions(this)    is the Functions class btw
But the problem is that it only executes the first piece of data that arrives and then it wont invoke the new data.

Can anyone help me please?
May i suggest using delegation over reflection? As its much faster ;)

Example 1

The following is a simple example of declaring and using a delegate.

PHP:
// keyword_delegate.cs
// delegate declaration
delegate void MyDelegate(int i);

class Program
{
   public static void Main()
   {
      TakesADelegate(new MyDelegate(DelegateFunction));
   }

   public static void TakesADelegate(MyDelegate SomeFunction)
   {
      SomeFunction(21);
   }
   
   public static void DelegateFunction(int i)
   {
      System.Console.WriteLine("Called by delegate with number: {0}.", i);
   }
}

Output

Called by delegate with number: 21.
Source: http://msdn.microsoft.com/en-us/library/900fyy8e(VS.71).aspx
 
So actually, you can use the

Code:
delegate void MyDelegate(int i);
for multiple voids (wich passes an integer) ?
Like you have to declare the delegate once and you can use it for all the invokes that need an integer?

Thx in advance
 
Back