楼主可以试试我的方法: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection;namespace test { class Program { static void Main(string[] args) { //目标类型字符串 string s = "System.Double"; Type t = Type.GetType(s); int i = 4; //获取Parse方法 MethodInfo method = t.GetMethod("Parse",new Type[] { typeof(string)}); object[] parameters = new object[] { i.ToString() }; //调用方法,并传入参数,用一个object接收返回值 object returnValue = method.Invoke(null, parameters); Console.WriteLine(returnValue); Console.ReadKey(); } } }
可以将 string sss = "double d = (" + s +")i;"; 写到一个文件中,再粘出来就是 double d = (double)i;
string s = "System.Double"; int i = 2; dynamic value = Convert.ChangeType(i, Type.GetType(s)); value += 1.1; Console.WriteLine(value); 试试dynamic
string s = "double";
if(s.ToLower().Contains("double"))
Convert.ToDouble(i);
ScriptEngine 是 Roslyn 提供的呀。
下面这篇文章有点类似:
http://www.codeproject.com/Articles/338088/Using-Roslyn-ScriptEngine-for-a-ValueConverter-to
Type t=Type.GetType(/*你的类型名称*/)
有了这个,什么实例化,函数调用都属于后续之作
if else判断s到底是什么,反正类型可以枚举全
// 摘要:
// 返回一个指定类型的对象,该对象的值等效于指定的对象。
//
// 参数:
// value:
// 用于实现 System.IConvertible 接口的对象。
//
// conversionType:
// 要返回的对象的类型。
//
// 返回结果:
// 一个类型为 conversionType 的对象,其值等效于 value。- 或 -如果 value 是 null,且 conversionType
// 不是值类型,则为 null 引用(Visual Basic 中为 Nothing)。
//
// 异常:
// System.InvalidCastException:
// 不支持此转换。- 或 -value 为 null,而且 conversionType 是值类型。- 或 -value 不实现 System.IConvertible
// 接口。
//
// System.FormatException:
// conversionType 无法识别value的格式。
//
// System.OverflowException:
// value 表示的数字超出 conversionType 的范围。
//
// System.ArgumentNullException:
// conversionType 为 null。
public static object ChangeType(object value, Type conversionType);
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;namespace test
{
class Program
{
static void Main(string[] args)
{
//目标类型字符串
string s = "System.Double";
Type t = Type.GetType(s);
int i = 4;
//获取Parse方法
MethodInfo method = t.GetMethod("Parse",new Type[] { typeof(string)});
object[] parameters = new object[] { i.ToString() }; //调用方法,并传入参数,用一个object接收返回值
object returnValue = method.Invoke(null, parameters);
Console.WriteLine(returnValue);
Console.ReadKey();
}
}
}
写到一个文件中,再粘出来就是
double d = (double)i;
dynamic value = Convert.ChangeType(i, Type.GetType(s));
value += 1.1;
Console.WriteLine(value);
试试dynamic