怎么实现函数ChangeType,能达到如下功能?
int a= ChangeType( Object,a.GetType());
string b= ChangeType( Object,b.GetType());
bool c= ChangeType( Object,c.GetType());
就是根据前面的类型自动实现转换Object为该强类型?
支持常规的int,String,bool等类型就可以.
.Net2.0实现,和.Net3.0以上实现.注意:
Convert.ChangeType实现不了这个功能.
因为他还需要强制转换,如:int i = (int)Convert.ChangeType(d, typeof(int));
int a= ChangeType( Object,a.GetType());
string b= ChangeType( Object,b.GetType());
bool c= ChangeType( Object,c.GetType());
就是根据前面的类型自动实现转换Object为该强类型?
支持常规的int,String,bool等类型就可以.
.Net2.0实现,和.Net3.0以上实现.注意:
Convert.ChangeType实现不了这个功能.
因为他还需要强制转换,如:int i = (int)Convert.ChangeType(d, typeof(int));
解决方案 »
- 编程词典代码 导出Excel时出错 请高手指点一下谢谢。
- treeview选中节点后,treenode为什么没有变成蓝色背景?
- 浏览器兼容问题
- remoting在外网的访问问题
- VC++2005 中动态添加的控件的事件处理问题(C#一样可以解决,所以我到C#来提问)
- Excel中怎么插入一行(急)
- 控制台程序里面,Timer.BeginInit()/EndInit()会有什么作用吗?
- 全部的WindowsAPI怎么找到
- 作64位程序和32位程序有什么不同.应该注意什么?
- 正则的问题,提取。
- c#开发web时,如何添加现有的SQL数据库
- vc 用winPcap捕获的数据包与用c# socket接收到的数据包问题
{
result = (T)Convert.ChangeType(o, typeof(T));
}int a;
ChangeType<int>(o, a.GetType(), out a);
{
return T(obj);
}
{
static void Main(string[] args)
{
Object ob = 3;
int a = ChangeType<int>(ob);
Console.WriteLine(a.GetType());
Console.WriteLine(a);
Console.WriteLine(ChangeType<string>(ob).GetType());
}
static T ChangeType<T>(object oj)
{
T result = (T)Convert.ChangeType(oj, typeof(T));
return result;
} }
如果只需要转换的话把void函数就行,在里面把值赋给全局变量 到时候取就可以你不如这样问:怎么让一个函数既能返回int值又能返回string值,还能反回bool值
是这样吧?
开始我也这么想的, 但是这个 不符合他的要求ChangeType<int>(ob);
{
public Object convertValue(Map context, Object target, Member member, String propertyName, Object value, Class toType);
}
看ognl源代码,不知道NET有没有对应版本。改改即可!
{
case "System.Int32":
return (int)Convert.ChangeType(obj,int);
case "System.Boolean":
return (bool)Convert.ChangeType(obj,bool);
default:
return obj.ToString();
}但是,你的函数签名怎么写?写成int ChangeType(object obj,Type detailType),返回不了bool类型
写成bool ChangeType(object obj,Type detailType),返回不了int类型
两个都写上去?编译器不认写成object ChangeType(object obj,Type detailType),你返回的是object,仍然要再去转换成int或者bool楼主,你这个问题我之前也想过,但是最后发现这个在目前无法实现
如javascript之类的弱类型语言,应该能实现
static object ChangeType(object o,Type t)
{
if (t.Name == "Int32")
return (int)o;
else
{
return (string)o;
}
}
调用时
var a=ChangeType(1,typeof(int));
Console.WriteLine(a.GetType());
Console.ReadKey();
string b= ChangeType( Object,b.GetType());
bool c= ChangeType( Object,c.GetType());就是根据前面的类型自动实现转换Object为该强类型?支持常规的int,String,bool等类型就可以..Net2.0实现,和.Net3.0以上实现.注意:
Convert.ChangeType实现不了这个功能.
因为他还需要强制转换,如:int i = (int)Convert.ChangeType(d, typeof(int));
class Program
{
static void Main(string[] args)
{
Object ob = 3;
int a = ChangeType<int>(ob);
Console.WriteLine(a.GetType());
Console.WriteLine(a);
Console.WriteLine(ChangeType<string>(ob).GetType());
}
static T ChangeType<T>(object oj)
{
T result = (T)Convert.ChangeType(oj, typeof(T));
return result;
} }这个好像是对的 学习了
{
return (int)obj ;
}
public static string ChangeType(object obj, string input)
{
return (string)obj;
}
public static bool ChangeType(object obj, bool input)
{
return (bool)obj;
}
public static DateTime ChangeType(object obj, DateTime input)
{
return (DateTime)obj;
}
{
object obj; obj = 123;
int a = ChangeType(obj, typeof(int));
Console.WriteLine(a); obj = "abc";
string b = ChangeType(obj, typeof(string));
Console.WriteLine(b); obj = true;
bool c = ChangeType(obj, typeof(bool));
Console.WriteLine(c);
}static MixedType ChangeType(object value, Type type)
{
return new MixedType(value, type);
}struct MixedType
{
private object _value;
private Type _type; public MixedType(object value, Type type)
{
_value = value;
_type = type;
} public static implicit operator int(MixedType mixedType)
{
return (int)Convert.ChangeType(mixedType._value, mixedType._type); ;
} public static implicit operator bool(MixedType mixedType)
{
return (bool)Convert.ChangeType(mixedType._value, mixedType._type); ;
} public static implicit operator string(MixedType mixedType)
{
return (string)Convert.ChangeType(mixedType._value, mixedType._type); ;
}
}
{
return Activator.CreateInstance(t);
}不知道你转换的逻辑打算怎么做。
我看了题目,以为 ChangeType 方法的第二个参数 是一个 System.Type类型,如果是那样的话
在C#中,只好用泛型
在IL里面,倒是可以,IL倒是能够依照不同的返回值来识别重载函数
没想到.. ... 这有点欺负人了啊~~~
就是根据前面的类型自动实现转换Object为该强类型如果你是想用泛型转换,如楼上那几位所说,那么你这个题目也太简单了,谈不上“考考你”。我不太明白你的意思,如果用泛型,那这题也太简单了吧。T changeType<T>(object o)
{
T result = (T)Convert.ChangeType(oj, typeof(T));
return result;}
如果你还需要转换成其他100种类型呢?你写100个方法重载,如果1万个类型呢?
public static int ChangeType (object obj,int input)
{
return (int)obj ;
}
public static string ChangeType(object obj, string input)
{
return (string)obj;
}
public static bool ChangeType(object obj, bool input)
{
return (bool)obj;
}
public static DateTime ChangeType(object obj, DateTime input)
{
return (DateTime)obj;
}
参数可以是类名??
可a.GetType() 的类型是 Type,你的答案第二个参数的类型根本不是Type