今天做项目是需要从xml中取DataTable的列类型,需要进行转换但是没有找到Convert.StringToType
private Type ConvertType(string STRtype)
{
var type = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(a=> a.GetTypes())
.Where(u => STRtype.Contains(u.Name))
.ToList();
return type[0];
}
或者
public object ConvertObject(string className)
{
// 获取程序集中的类型
Type type = Type.GetType(className);
if (type != null)
{
// 使用 Activator 创建对象
object obj = Activator.CreateInstance(type);
return obj;
}
return null;
}