Pass the method to another method that requires a method name

Below is the code used to pass a reference to a method containing strings as parameters, the purpose of this question is to use generics to deny the need to determine the actual type!

Impossible<ExampleSource, string>.Example(c => c.NonString); //does not work
Impossible<ExampleSource, string>.Example<int>(c => c.NonString); //does work 

The idea is to make the first call to "NonString" without having to determine the type of parameter or declare a new function in Impossible, which accepts Func <int, TResult>.

public static void Example(Expression<Func<TSource, Func<int, TResult>>> function) 
    { Process(function as MethodCallExpression); } //invalid solution...

In Java, this can be achieved with Func <?, TResult>

public class Impossible<TSource, TResult>
{
    public static void Example(Expression<Func<TSource, Func<TResult>>> function) 
        { Process(function as MethodCallExpression); }
    public static void Example(Expression<Func<TSource, Func<string, TResult>>> function) 
        { Process(function as MethodCallExpression); }
    public static void Example(Expression<Func<TSource, Func<string, string, TResult>>> function) 
        { Process(function as MethodCallExpression); }

    public static void Example<T1>(Expression<Func<TSource, Func<T1, TResult>>> function)
    { Process(function as MethodCallExpression); }
    public static void Example<T1, T2>(Expression<Func<TSource, Func<T1, T2, TResult>>> function)
    { Process(function as MethodCallExpression); }

    private static void Process(MethodCallExpression exp)
    {
        if (exp == null) return;
        Console.WriteLine(exp.Method.Name);
    }
}

public class ExampleSource
{
    public string NoParams() { return ""; }
    public string OneParam(string one) { return ""; }
    public string TwoParams(string one, string two) { return ""; }
    public string NonString(int i) { return ""; }
}

public class Consumer
{
    public void Argh()
    {
        Impossible<ExampleSource, string>.Example(c => c.NoParams);
        Impossible<ExampleSource, string>.Example(c => c.OneParam);
        Impossible<ExampleSource, string>.Example(c => c.TwoParams);
        Impossible<ExampleSource, string>.Example<int>(c => c.NonString);
        Impossible<ExampleSource, string>.Example(c => c.NonString); //MAKE THIS WORK
    }
}
+3
source share
5 answers

Its more like this is impossible :(

0
source

: , #. , , , , int. , .

, 14.5.5.1 # Language Specification. , , , , . 25, , .

, . , - .

+3

, , , :

# fieldof methodof?

, , ( ).

0

, , . , , . , # ...

public class Test{
     public int DoMethod(){ return 2; }
     public string DoMethod() { return "Name"; }
} 
Test t;
int n = t.DoMethod();  // 1st method
string txt = t.DoMethod(); // 2nd method
object x = t.DoMethod(); // DOOMED ... which one??

, , .

, ...

public class Test{
    public T ReturnEmpty<T>() { return default(T); }
    public T ReturnEmpty<T>(T x) { return x; }
}
Test t = new Test();
int n = t.ReturnEmpty(); <--- does not work.. 
                              the reson check next statement...
object k = t.ReturnEmpty(); <--- which one...
int l = t.ReturnEmpty<int>();  <--- this is correct....
string m = t.ReturnEmpty("Do Test"); <-- this is correct...

, , , , . , , " ", . .

, , , .

0

Impossible < > , <int, TResult>

public static void Example(Expression<Func<TSource, Func<int, TResult>>> function)
{ Process(function as MethodCallExpression); }
0

Source: https://habr.com/ru/post/1714582/


All Articles