如何將一個(gè)對(duì)象轉(zhuǎn)換為指定Type類(lèi)型???我這有個(gè)方法。如果是普通的對(duì)象,并且有默認(rèn)的無(wú)參數(shù)構(gòu)造函數(shù),轉(zhuǎn)換目前沒(méi)發(fā)現(xiàn)問(wèn)題。但如果是集合(比如數(shù)組,List)轉(zhuǎn)換不了。。這個(gè)方法是用來(lái)反射調(diào)用方法的,而參數(shù)是反序列化JSON得來(lái)的。。比如我有個(gè)方法:
public void Test(int[] numbers) // 這個(gè)可能不是int[],可能是任意類(lèi)型,但是我能夠得到他的Type實(shí)例
{
}
我要反射調(diào)用這個(gè)方法。參數(shù)是由客戶(hù)端以Json格式發(fā)送過(guò)來(lái)的。
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并不是這樣獲取的,由于比較麻煩,這里為了容易說(shuō)清楚,直接寫(xiě)死
現(xiàn)在我要調(diào)用最下面那個(gè)ConvertObject方法來(lái)把object[]?轉(zhuǎn)換為?int[]
object o = this.ConvertObject(numbers,type); // 這個(gè)numbers是通過(guò)JSON獲取到的參數(shù)
?
其實(shí)為了說(shuō)清楚,說(shuō)了一堆廢話(huà),只要看到下面這個(gè)方法,就知道我想干什么了。。。
1 ///
2 /// 將一個(gè)對(duì)象轉(zhuǎn)換為指定類(lèi)型
3 ///
4 /// 待轉(zhuǎn)換的對(duì)象
5 /// 目標(biāo)類(lèi)型
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ì)象的類(lèi)型與目標(biāo)類(lèi)型兼容,則無(wú)需轉(zhuǎn)換
14 {
15 return obj;
16 }
17 else if ((underlyingType ?? type).IsEnum) // 如果待轉(zhuǎn)換的對(duì)象的基類(lèi)型為枚舉
18 {
19 if (underlyingType != null && string.IsNullOrEmpty(obj.ToString())) // 如果目標(biāo)類(lèi)型為可空枚舉,并且待轉(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)類(lèi)型的基類(lèi)型實(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
?
C#類(lèi)型轉(zhuǎn)換(反射)
開(kāi)滿(mǎn)天機(jī)
2018-12-07 07:49:41