Mic*_*sov 5 c# dictionary func
考虑以下:
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();
}
Run Code Online (Sandbox Code Playgroud)
我可以手动填写字典.但是可以动态添加吗?
我如何将泛型方法转换为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();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
191 次 |
| 最近记录: |