Amin AmiriDarban
Amin AmiriDarban

Reputation: 2068

How Can i call other functions in a class from special function(Just with their string names) in C#?

I have a Class by name of GoldClass. It has four methods by names F(),S(),TH(),Fo(). In the methods FO(). I want to call three other methods (F,S,TH) but just use their names like this.

public void Fo()
{
    list <string> FuncsList=new list<string>();
    FuncsList.Add("F");
    FuncsList.Add("S");
    FuncsList.Add("TH");

    //NOW I WANT TO CALL THEM BUT USE MY STRINGS IN MY FUNCSLIST  
}

I used Reflection at first ... but I am assigning some values to public variables in my class..and when reflection make new instance all my data in public variables is lost.

Upvotes: 0

Views: 159

Answers (5)

Casperah
Casperah

Reputation: 4564

You have two options: Either use reflection. Or use some kind of switch statement, the later is faster.

public void Fo()
{
    list <string> FuncsList=new list<string>();
    FuncsList.Add("F");
    FuncsList.Add("S");
    FuncsList.Add("TH");

    foreach(String name in FuncsList)
        CallNamedMethod(name);
}

private void CallNamedMethod(string name)
{
    switch(name)
    {
        case "F":
            F();
            return;

        case "S":
            S();
            return;

        case "TH":
            TH();
            return;

        default:
            throw new InvalidArgumentException();
    }
}

If you have a huge amount of different methods that you can call, you may create a dictionary with names and delegates.

Good luck with your quest.

Upvotes: 0

Emre
Emre

Reputation: 1

Call Direct assembly where they belongs c# cannot know is their references

Upvotes: -1

Shreyas
Shreyas

Reputation: 9

You can invoke a function with a string. Check this link for more information http://msdn.microsoft.com/en-us/library/system.type.invokemember.aspx

Upvotes: 1

Moo-Juice
Moo-Juice

Reputation: 38820

You could use a dictionary of strings and actions for this:

public class Example
{
    private Dictionary<string, Action> _map = new Dictionary<string, Action>();

    public Example()
    {
        _map["A"] = A;
        _map["B"] = B;
        _map["C"] = C;
    }

    public void A() { }
    public void B() { }
    public void C() { }

    public void CallByString(string func)
    {
        if(_map.ContainsKey(func))
            _map[func]();
    }
}

Usage:

Example example = new Example();
example.CallByString("A");

Upvotes: 0

Eoin Campbell
Eoin Campbell

Reputation: 44308

Is there any particular reason, you need to store them as strings. If they're all parameterless void methods in the same class, you could store them as Actions

void Main()
{
    A();
}

public void A()
{
    List<Action> list = new List<Action>();
    list.Add(B);
    list.Add(C);
    list.Add(D);

    foreach(Action action in list){
        action();
    }
}


public void B()
{
    Console.WriteLine("B Called");
}


public void C()
{
    Console.WriteLine("C Called");
}


public void D()
{
    Console.WriteLine("D Called");
}

Having said that, It's not at all clear why you need to do this at all. Why do you need to do this dynamically. would the following not suffice.

public void FO()
{
    F();
    S();
    TH();
}

Upvotes: 3

Related Questions