Clover.NET coverage report - Coverage for s2container.net

Coverage timestamp: 2006年5月30日 11:21:29

File Stats: LOC: 155   Methods: 6
NCLOC: 92 Classes: 1
 
Source File Conditionals Statements Methods TOTAL
Seasar.Framework.Util\ConversionUtil.cs 25.0% 45.2% 50.0% 39.6%
coverage coverage
1   #region Copyright
2   /*
3   * Copyright 2005 the Seasar Foundation and the Others.
4   *
5   * Licensed under the Apache License, Version 2.0 (the "License");
6   * you may not use this file except in compliance with the License.
7   * You may obtain a copy of the License at
8   *
9   * http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
14   * either express or implied. See the License for the specific language
15   * governing permissions and limitations under the License.
16   */
17   #endregion
18  
19   using System;
20   using System.Data.SqlTypes;
21   using System.Reflection;
22   using Nullables;
23  
24   namespace Seasar.Framework.Util
25   {
26   /// <summary>
27   /// 型の変換を行うクラス
28   /// </summary>
29   public sealed class ConversionUtil
30   {
31 0 private ConversionUtil()
32   {
33   }
34  
35   /// <summary>
36   /// 渡されたオブジェクトを目的のTypeに変換する
37   /// </summary>
38   /// <param name="o">変換するオブジェクト</param>
39   /// <param name="targetType">目的のType</param>
40   /// <returns>変換されたオブジェクト</returns>
41 0 public static object ConvertTargetType(object o, Type targetType)
42   {
43   object ret = null;
44  
45   if(typeof(IConvertible).IsAssignableFrom(targetType))
46   {
47   ret = ConvertConvertible(o, targetType);
48   }
49   else if(typeof(INullable).IsAssignableFrom(targetType))
50   {
51   ret = ConvertNullable(o, targetType);
52   }
53   else if(typeof(INullableType).IsAssignableFrom(targetType))
54   {
55   ret = ConvertNullableType(o, targetType);
56   }
57   else if(o == DBNull.Value)
58   {
59   ret = null;
60   }
61   else
62   {
63   ret = o;
64   }
65  
66   return ret;
67   }
68  
69   /// <summary>
70   /// オブジェクトをINullableを実装するTypeに変換する
71   /// </summary>
72   /// <param name="o">変換するオブジェクト</param>
73   /// <param name="targetType">INullableを実装するType</param>
74   /// <returns>変換されたオブジェクト</returns>
75 39 public static object ConvertNullable(object o, Type targetType)
76   {
77 39 INullable ret = null;
78  
79 39 if(o == null || o == DBNull.Value)
80   {
81 26 ret = (INullable) targetType.InvokeMember("Null", BindingFlags.GetField,
82   null, null, null);
83   }
84   else
85   {
86 13 Type paramType = GetValueType(targetType);
87 13 ret = (INullable) Activator.CreateInstance(targetType,
88   new object[] { Convert.ChangeType(o, paramType) });
89   }
90  
91 39 return ret;
92   }
93  
94   /// <summary>
95   /// オブジェクトをINullableTypeを実装するTypeに変換する
96   /// </summary>
97   /// <param name="o">変換するオブジェクト</param>
98   /// <param name="targetType">INullableTypeを実装するType</param>
99   /// <returns>変換されたオブジェクト</returns>
100 36 public static object ConvertNullableType(object o, Type targetType)
101   {
102 36 INullableType ret = null;
103  
104 36 if(o == null || o == DBNull.Value)
105   {
106 24 ret = (INullableType) targetType.InvokeMember("Default", BindingFlags.GetField,
107   null, null, null);
108   }
109   else
110   {
111 12 Type paramType = GetValueType(targetType);
112 12 ret = (INullableType) Activator.CreateInstance(targetType,
113   new object[] { Convert.ChangeType(o, paramType) });
114   }
115  
116 36 return ret;
117   }
118  
119   /// <summary>
120   /// オブジェクトをIConvertibleを実装するTypeに変換する
121   /// </summary>
122   /// <param name="o">変換するオブジェクト</param>
123   /// <param name="targetType">IConvertibleを実装するType</param>
124   /// <returns>変換されたオブジェクト</returns>
125 0 public static object ConvertConvertible(object o, Type targetType)
126   {
127   object ret = null;
128  
129   if(o == null || o == DBNull.Value)
130   {
131   if(!targetType.Equals(typeof(string)))
132   {
133   ret = Convert.ChangeType(decimal.Zero, targetType);
134   }
135   }
136   else
137   {
138   ret = Convert.ChangeType(o, targetType);
139   }
140  
141   return ret;
142   }
143  
144   /// <summary>
145   /// プロパティ名が"Value"であるプロパティのTypeを返す
146   /// </summary>
147   /// <param name="targetType">Type</param>
148   /// <returns></returns>
149 25 private static Type GetValueType(Type targetType)
150   {
151 25 PropertyInfo pi = targetType.GetProperty("Value");
152 25 return pi.PropertyType;
153   }
154   }
155   }