mercredi 24 juin 2020

Reconstruct MethodInfo of function returned by another function for later execution

Considering the following code:

void Main()
{
    var f1 = (IO<int, int>)Functions.AddOne;
    f1.GetType().DeclaringType.AssemblyQualifiedName.Dump("AddOne type");
    f1.GetMethodInfo().DeclaringType.AssemblyQualifiedName.Dump("AddOne MethodInfo type");
    f1.GetMethodInfo().Name.Dump("AddOne MethodInfo name");
    
    Type.GetType(f1.GetType().DeclaringType.AssemblyQualifiedName).AssemblyQualifiedName.Dump("AddOne reconstructed type");
    var type1b = Type.GetType(f1.GetMethodInfo().DeclaringType.AssemblyQualifiedName);
    type1b.AssemblyQualifiedName.Dump("AddOne reconstructed MethodInfo declaring type");
    type1b.GetMember(f1.GetMethodInfo().Name).First().Name.Dump("AddOne reconstructed MethodInfo name");
    
    var f2 = Curry<int, int>(Functions.AddOne, 6);
    f2.GetType().DeclaringType.AssemblyQualifiedName.Dump("Curried type");
    f2.GetMethodInfo().DeclaringType.AssemblyQualifiedName.Dump("Curried MethodInfo type");
    f2.GetMethodInfo().Name.Dump("Curried MethodInfo name");
    
    Type.GetType(f2.GetType().DeclaringType.AssemblyQualifiedName).AssemblyQualifiedName.Dump("Curried reconstructed type");
    var type2b = Type.GetType(f2.GetMethodInfo().DeclaringType.AssemblyQualifiedName);
    type2b.AssemblyQualifiedName.Dump("Curried reconstructed MethodInfo declaring type");
    type2b.GetMember(f1.GetMethodInfo().Name).Dump("Curried reconstructed MethodInfo");
}

public static class Functions {
    public static void AddOne(int a, out int b) {
        b = a + 1;
    }
}

public delegate void O<TO1>(out TO1 out1);
public delegate void IO<TI1, TO1>(TI1 in1, out TO1 out1);

public static O<TO1> Curry<TI1, TO1>(IO<TI1, TO1> f, TI1 in1) => (out TO1 out1) => f(in1, out out1);

f1 is correctly reconstructed:

AddOne type:

UserQuery, LINQPadQuery, Version=1.0.0.713, Culture=neutral, PublicKeyToken=null

AddOne MethodInfo type:

UserQuery+Functions, LINQPadQuery, Version=1.0.0.713, Culture=neutral, PublicKeyToken=null

AddOne MethodInfo name:

AddOne

AddOne reconstructed type:

UserQuery, LINQPadQuery, Version=1.0.0.713, Culture=neutral, PublicKeyToken=null

AddOne reconstructed MethodInfo declaring type:

UserQuery+Functions, LINQPadQuery, Version=1.0.0.713, Culture=neutral, PublicKeyToken=null

AddOne reconstructed MethodInfo name:

AddOne

So I can re-execute it at a later execution of my program.

However, f2 is not:

Curried type:

UserQuery, LINQPadQuery, Version=1.0.0.713, Culture=neutral, PublicKeyToken=null

Curried MethodInfo type:

UserQuery+<>c__DisplayClass8_0`2[[System.Int32, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Int32, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], LINQPadQuery, Version=1.0.0.713, Culture=neutral, PublicKeyToken=null

Curried MethodInfo name:

b__0

Curried reconstructed type:

UserQuery, LINQPadQuery, Version=1.0.0.713, Culture=neutral, PublicKeyToken=null

Curried reconstructed MethodInfo declaring type:

UserQuery+<>c__DisplayClass8_0`2[[System.Int32, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Int32, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], LINQPadQuery, Version=1.0.0.713, Culture=neutral, PublicKeyToken=null

Curried reconstructed MethodInfo:

(0 items)

Is there a way to reconstruct such a function returned from another function such that it can be re-executed at a later execution of my program?





Aucun commentaire:

Enregistrer un commentaire