Chr*_*ris 2 c# asp.net function dynamic
我想创建一个具有两个参数的函数
public **XYZ** GetOputput(string strToConvert, **ABC**)
Run Code Online (Sandbox Code Playgroud)
我想从这个函数,我将发送一个字符串到这个函数和我要转换此字符串的数据类型[Ex:Int32,Int64,datetime等..]和返回将与数据类型I相同已作为输入参数发送.
我希望在我的函数中有这样的东西:
switch(//what is the data type)
case: //if data type is date than do something and return date
case: //if data type is int than do something and return int
Run Code Online (Sandbox Code Playgroud)
我不知道如何动态发送,比较和返回数据类型.协助我
如何使用通用函数.
public T GetValue<T>(string value)
{
return (T)Convert.ChangeType(value, typeof(T), CultureInfo.InvariantCulture);
}
Run Code Online (Sandbox Code Playgroud)
因此,要从字符串中获取int,请尝试以下操作:
int x = GetValue<int>("1234");
Run Code Online (Sandbox Code Playgroud)