is it possible to make generic function in c# that get as input some class and method (of the class) and parameters to the method ( and maybe the result type ) and make instance of that class and call to the function of the class with the parameters and return the result?
            Asked
            
        
        
            Active
            
        
            Viewed 1,355 times
        
    0
            
            
        - 
                    You should be able to do it with reflection – Flat Eric Jun 06 '14 at 13:20
 - 
                    can you add code please... – ilay zeidman Jun 06 '14 at 13:20
 - 
                    See here: http://msdn.microsoft.com/library/system.type.getconstructor%28v=vs.110%29.aspx and http://msdn.microsoft.com/library/system.type.getconstructor%28v=vs.110%29.aspx – Flat Eric Jun 06 '14 at 13:23
 - 
                    When you say "get as input... method", do you mean a string containing the method name? – Ben Aaronson Jun 06 '14 at 13:28
 
4 Answers
4
            Sure.
public class MyClass
{
    public class Test
    {
        public int TestMethod(int a, int b)
        {
            return a + b;
        }
    }
    public static void Main()
    {
        int result = ExecuteMethod<Test, int>("TestMethod", 1, 2);
        Console.Read();
    }
    public static TResult ExecuteMethod<TClass, TResult>(string methodName, params object[] parameters)
    {
        // Instantiate the class (requires a default parameterless constructor for the TClass type)
        var instance = Activator.CreateInstance<TClass>();
        // Gets method to execute
        var method = typeof(TClass).GetMethod(methodName, BindingFlags.Public | BindingFlags.Instance);
        // Executes and returns result
        return (TResult)method.Invoke(instance, parameters);
    }
}
        ken2k
        
- 48,145
 - 10
 - 116
 - 176
 
2
            
            
        Unless Reflection is your absolute option, use one of the following delegates:
Action<T>: Will let you execute a method that does not return a value. There are several overloads that will let you pass in additional arguments.Func<TResult>: Will let you execute a method that returns a result of typeTResult. There are more overloads that will let you pass in additional arguments. They all follow the syntaxFunc<T1, T2, T3, TResult>and so on.And finally, you can define your own delegate.
        Mario J Vargas
        
- 1,185
 - 6
 - 12
 
1
            
            
        Yes it's possible. You can do that with reflection.
Here you have a few useful links
1
            
            
        Here's how you create an instance of a class using reflection and then call a method on that class.
Assuming you have a class:
public class MyType
{
    public void DoSomething()
    {
        // do stuff here
    }
}
You could do the following:
Type instanceType = Type.GetType("MyType");
object instance = Activator.CreateInstance(instanceType);
MethodInfo method = instanceType.GetMethod("MethodName");
object returnValue = method.Invoke(instance, new object[] { /* paramaters go here */ });
        Ian P
        
- 12,840
 - 6
 - 48
 - 70