Dynamically invoking any function by passing function name as string

后端 未结 5 583
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-01 03:54

How do I automate the process of getting an instance created and its function executed dynamically?

Thanks

Edit: Need an option to pass parameters too. Thank

5条回答
  •  孤城傲影
    2020-12-01 04:14

    To pass the parameters dynamically Here I have taken params string[] args, because different functions have different number of parameters so.

    public void Invoke(string typeName,string functionName,params string[] args)
        {
    
         Type type = Type.GetType(typeName);
         dynamic c=Activator.CreateInstance(type);
         //args contains the parameters(only string type)
         type.InvokeMember(functionName,BindingFlags.InvokeMethod,null,c,args);   
    
        }
    

提交回复
热议问题