如何將一個(gè)對(duì)象轉(zhuǎn)換為指定Type類型???我這有個(gè)方法。如果是普通的對(duì)象,并且有默認(rèn)的無參數(shù)構(gòu)造函數(shù),轉(zhuǎn)換目前沒發(fā)現(xiàn)問題。但如果是集合(比如數(shù)組,List)轉(zhuǎn)換不了。。這個(gè)方法是用來反射調(diào)用方法的,而參數(shù)是反序列化JSON得來的。。比如我有個(gè)方法:
public void Test(int[] numbers) // 這個(gè)可能不是int[],可能是任意類型,但是我能夠得到他的Type實(shí)例
{
}
我要反射調(diào)用這個(gè)方法。參數(shù)是由客戶端以Json格式發(fā)送過來的。
Stream stream = this.Request.InputStream;
Encoding encoding = this.Request.ContentEncoding;
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
string postData = encoding.GetString(bytes);
JavaScriptSerializer serializer = new JavaScriptSerializer();
object arg = serializer.DeserializeObject(postData);
object[] numbers = arg as object[]// 獲取參數(shù),雖然是object數(shù)組,但是元素都是int的。
但是得到的是object數(shù)組。
參數(shù)的Type我用其他的方式能夠獲取。。
Type type = typeof(int[]);// 這個(gè)Type并不是這樣獲取的,由于比較麻煩,這里為了容易說清楚,直接寫死
現(xiàn)在我要調(diào)用最下面那個(gè)ConvertObject方法來把object[]?轉(zhuǎn)換為?int[]
object o = this.ConvertObject(numbers,type); // 這個(gè)numbers是通過JSON獲取到的參數(shù)
?
其實(shí)為了說清楚,說了一堆廢話,只要看到下面這個(gè)方法,就知道我想干什么了。。。
1 ///
2 /// 將一個(gè)對(duì)象轉(zhuǎn)換為指定類型
3 ///
4 /// 待轉(zhuǎn)換的對(duì)象
5 /// 目標(biāo)類型
6 /// 轉(zhuǎn)換后的對(duì)象
7 private object ConvertObject(object obj, Type type)
8 {
9 if (type == null) return obj;
10 if (obj == null) return type.IsValueType ? Activator.CreateInstance(type) : null;
11
12 Type underlyingType = Nullable.GetUnderlyingType(type);
13 if (type.IsAssignableFrom(obj.GetType())) // 如果待轉(zhuǎn)換對(duì)象的類型與目標(biāo)類型兼容,則無需轉(zhuǎn)換
14 {
15 return obj;
16 }
17 else if ((underlyingType ?? type).IsEnum) // 如果待轉(zhuǎn)換的對(duì)象的基類型為枚舉
18 {
19 if (underlyingType != null && string.IsNullOrEmpty(obj.ToString())) // 如果目標(biāo)類型為可空枚舉,并且待轉(zhuǎn)換對(duì)象為null 則直接返回null值
20 {
21 return null;
22 }
23 else
24 {
25 return Enum.Parse(underlyingType ?? type, obj.ToString());
26 }
27 }
28 else if (typeof(IConvertible).IsAssignableFrom(underlyingType ?? type)) // 如果目標(biāo)類型的基類型實(shí)現(xiàn)了IConvertible,則直接轉(zhuǎn)換
29 {
30 try
31 {
32 return Convert.ChangeType(obj, underlyingType ?? type, null);
33 }
34 catch
35 {
36 return underlyingType == null ? Activator.CreateInstance(type) : null;
37 }
38 }
39 else
40 {
41 TypeConverter converter = TypeDescriptor.GetConverter(type);
42 if (converter.CanConvertFrom(obj.GetType()))
43 {
44 return converter.ConvertFrom(obj);
45 }
46 ConstructorInfo constructor = type.GetConstructor(Type.EmptyTypes);
47 if (constructor != null)
48 {
49 object o = constructor.Invoke(null);
50 PropertyInfo[] propertys = type.GetProperties();
51 Type oldType = obj.GetType();
52 foreach (PropertyInfo property in propertys)
53 {
54 PropertyInfo p = oldType.GetProperty(property.Name);
55 if (property.CanWrite && p != null && p.CanRead)
56 {
57 property.SetValue(o, ConvertObject(p.GetValue(obj, null), property.PropertyType), null);
58 }
59 }
60 return o;
61 }
62 }
63 return obj;
64 }
ConvertObject
?
2 回答

慕田峪4524236
TA貢獻(xiàn)1875條經(jīng)驗(yàn) 獲得超5個(gè)贊
可以序列化成 特定類型,然后再反序列化成你想要的類型,不知道是不是你想要的
代碼:可以搜索序列化和反序列化,代碼很少
- 2 回答
- 0 關(guān)注
- 858 瀏覽
添加回答
舉報(bào)
0/150
提交
取消