foreach (var filter in filters)
{
var filterType = typeof(Filters);
var method = filterType.GetMethod(filter, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Static);
if (method != null)
{
var parameters = method.GetParameters();
Type paramType = parameters[0].ParameterType;
value = (string)method.Invoke(null, new[] { value });
}
}
如何将 value
转换为 paramType
? value
是一个 string
,paramType
可能只是一个基本类型,如 int
、string
,或者 float
。如果无法进行转换,我很乐意抛出异常。
最佳答案
您使用的类型都实现了 IConvertible
。因此,您可以使用 ChangeType
。
value = Convert.ChangeType(method.Invoke(null, new[] { value }), paramType);
关于c# - 将变量转换为仅在运行时已知的类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4010144/