如何动态填充 Func<T> 的字典
How to populate dictionary of Func<T> dynamically
考虑以下因素:
public interface ISomething
{
string Something();
}
public class A : ISomething
{
public string Something()
{
return "A";
}
}
public class B : ISomething
{
public string Something()
{
return "B";
}
}
public class Helper
{
private static readonly Dictionary<string, Func<string, string>> Actions =
new Dictionary<string, Func<string, string>>(StringComparer.OrdinalIgnoreCase);
public Helper()
{
Actions["A"] = DoSomething<A>;
Actions["B"] = DoSomething<B>;
var types = Assembly.GetExecutingAssembly().GetTypes().Where(t => t.GetInterfaces().Contains(typeof(ISomething)));
foreach (var type in types)
{
Console.WriteLine(type.Name);
// Actions[type.Name] = ????;
}
}
public static string DoSomething<T>(string data) where T : ISomething
{
T obj = JsonConvert.DeserializeObject<T>(data);
// Some manipulations
return obj.Something();
}
}
void Main()
{
var h = new Helper();
}
我可以手动填写字典。但是可以动态添加吗?
如何将泛型方法转换为 Func?
您可以创建一个 Expression
并将其编译为 Func<string, string>
:
public Helper()
{
var types = Assembly.GetExecutingAssembly().GetTypes().Where(t => t.GetInterfaces().Contains(typeof(ISomething)));
foreach (var type in types)
{
var data = Expression.Parameter(typeof(string), "data");
var call = Expression.Call(typeof(Helper), "DoSomething", new Type[] { type }, data);
var lambda = Expression.Lambda(call, data);
Actions[type.Name] = (Func<string, string>)lambda.Compile();
}
}
考虑以下因素:
public interface ISomething
{
string Something();
}
public class A : ISomething
{
public string Something()
{
return "A";
}
}
public class B : ISomething
{
public string Something()
{
return "B";
}
}
public class Helper
{
private static readonly Dictionary<string, Func<string, string>> Actions =
new Dictionary<string, Func<string, string>>(StringComparer.OrdinalIgnoreCase);
public Helper()
{
Actions["A"] = DoSomething<A>;
Actions["B"] = DoSomething<B>;
var types = Assembly.GetExecutingAssembly().GetTypes().Where(t => t.GetInterfaces().Contains(typeof(ISomething)));
foreach (var type in types)
{
Console.WriteLine(type.Name);
// Actions[type.Name] = ????;
}
}
public static string DoSomething<T>(string data) where T : ISomething
{
T obj = JsonConvert.DeserializeObject<T>(data);
// Some manipulations
return obj.Something();
}
}
void Main()
{
var h = new Helper();
}
我可以手动填写字典。但是可以动态添加吗?
如何将泛型方法转换为 Func?
您可以创建一个 Expression
并将其编译为 Func<string, string>
:
public Helper()
{
var types = Assembly.GetExecutingAssembly().GetTypes().Where(t => t.GetInterfaces().Contains(typeof(ISomething)));
foreach (var type in types)
{
var data = Expression.Parameter(typeof(string), "data");
var call = Expression.Call(typeof(Helper), "DoSomething", new Type[] { type }, data);
var lambda = Expression.Lambda(call, data);
Actions[type.Name] = (Func<string, string>)lambda.Compile();
}
}