C# 生成编译时返回类型未知的 lambda 表达式

Eke*_*ein 5 c# lambda return compile-time

假设我有以下方法:

public static String GetString(int a) {
    return "";
}

public static int GetInt(int a) {
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

现在我想为这些方法之一创建 lambda 表达式,我只在编译时知道以下内容:

方法信息 方法信息;上述方法之一的方法信息。

属性类型 属性类型;上述方法之一的返回类型的属性类型。

我在这里不能使用泛型类型,因为我不知道我希望在编译时调用哪个方法。

设想:

我有以下模型类:

public class Model {
    public String Name {
        get;set;
    }

    public int Number {
        get;set;
    }
}
Run Code Online (Sandbox Code Playgroud)

在运行时我希望向该模型注入信息。

public static void Inject<T>(T model) {
    foreach(PropertyInfo propertyInfo in typeof(T).GetProperties()) {
        Func<int, object> funcGet = GetValueFunc(propertyInfo.PropertyType);
        propertyInfo.SetValue(model, funcGet.Invoke(0));
    }
}

public static Func<int, object> GetValueFunc(Type propertyType) {
    MethodInfo methodInfo = // say I know the method info here mapped to the propertyType

    // this won't work since object isn't of either int or String
    var iParam = Expression.Parameter(typeof(int), "iParam");
    var call = Expression.Call(methodInfo, iParam);
    var lambda = Expression.Lambda<Func<int, object>>(call, iParam);
    return lambda.Compile();
}
Run Code Online (Sandbox Code Playgroud)

有没有办法真正做到这一点?

我知道你可以做 Expression.Convert(Expression.Parameter(typeof(object), "o"), propertyType); 如果您不知道运行时参数的类型。对于返回类型是否有类似的方法?

dec*_*cPL 2

好吧,您并没有完全转换“返回类型”,因为您没有修改现有的方法代码。您需要改为转换该方法调用的结果,这是完全可行的,几乎使用您所说的:

var call = Expression.Call(methodInfo, iParam);
var cast = Expression.Convert(call, typeof (Object));
var lambda = Expression.Lambda<Func<int, Object>>(cast, iParam);
Run Code Online (Sandbox Code Playgroud)