Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
How would I take a variable and use it like a method? I heard something about method invoking but I can't find anything good about it. If someone could just edit this code and make it work please?

C#
namespace test
{
    static class Program
    {
        public void Main()
        {
            time(0, "command1");
            time(5000, "command2");
        }

        public void command1(object source, ElapsedEventArgs e)
        {
            MessageBox.Show("command1, prepair for 5 second gap!");
        }

        public void command2(object source, ElapsedEventArgs e)
        {
            MessageBox.Show("command2, gap complete!");
        }

        public void time(int value, string choice)
        {
            System.Timers.Timer t = new System.Timers.Timer();
            t.Elapsed += new ElapsedEventHandler(choice);
            t.Interval = value;
            t.AutoReset = false;
            t.Start();
        }
    }
}



Thanks for any help.
Posted
Comments
john123451 3-Mar-11 19:40pm    
t.Elapsed += new ElapsedEventHandler(choice);
This is the line that throws an error.

You can do it easily, but you should not. Imagine somebody rename the method and all you system will be destroyed, even though it compiles.

Instead, you should create an interface. With Reflection, list all classes of the Assembly you need to find the class which implement its interface. When (and if) the class is found, find its constructor and invoke it (through System.Reflection.ConstructorInfo). You got the instance of class (the variable of the type System.Object) implementing your interface. Type cast this object to your interface. You got the reference to your object instance of the interface type. Call one the methods. You're done!

The method with interface is good and safe, because if anyone renames it in just one place or change it any incompatible way, compiler will immediately show you how to fix the problem.

—SA
 
Share this answer
 
v2
That sort of thing can be done, but I would not recommend it. If you need to do this then your design is probably wrong.

Take a look at Run-Time Code Generation I: Compile C#-Code using Microsoft.CSharp and System.CodeCom.Compiler[^] to see if it fits your requirements.
 
Share this answer
 
Comments
Albin Abel 3-Mar-11 20:56pm    
Yes. it is possible using reflection. But not recommended way. My 5
Sergey Alexandrovich Kryukov 4-Mar-11 3:32am    
There is a safe method; see my Answer.
--SA
Sergey Alexandrovich Kryukov 3-Mar-11 21:39pm    
Henry, you're absolutely right, my 5. The method is very bad.
In my Answer I explain why and describe my method based on interfaces, quite robust and not based on any strings; please see.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900