/// <summary> /// 得到某个数据类型的缺省值 /// </summary> /// <param name="type">类型</param> /// <returns>该类型的缺省值</returns> /// <remarks>如果该类型为引用类型,则返回null,否则返回值类型的缺省值。如Int32返回0,DateTime返回DateTime.MinValue</remarks> public static object GetTypeDefaultValue(System.Type type) { ExceptionHelper.FalseThrow <ArgumentNullException>(type != null, "type"); object result = null; if (type.IsValueType) { if (TypeDefaultValueCacheQueue.Instance.TryGetValue(type, out result) == false) { result = TypeCreator.CreateInstance(type); TypeDefaultValueCacheQueue.Instance.Add(type, result); } } else { result = null; } return(result); }
/// <summary> /// 根据Request的参数得到可收集参数的值 /// </summary> /// <param name="reqParams"></param> /// <param name="parameterType"></param> /// <returns></returns> private static object GetCollectedParameterInstance(NameValueCollection reqParams, System.Type parameterType) { object graph = TypeCreator.CreateInstance(parameterType); Dictionary <string, PropertyInfo> propertyDict = TypeFlattenHierarchyPropertiesCacheQueue.Instance.GetPropertyDictionary(parameterType); propertyDict.ForEach(kp => { PropertyInfo pi = kp.Value; if (pi.CanWrite) { object targetValue = ChangeRequestValueToTargetType(pi.Name, reqParams, pi.PropertyType); if (targetValue != null) { pi.SetValue(graph, targetValue, null); } } }); return(graph); }
/// <summary> /// 根据类型信息创建对象,该对象即使没有公有的构造方法,也可以创建实例 /// </summary> /// <param name="type">创建类型时的类型信息</param> /// <param name="constructorParams">创建实例的初始化参数</param> /// <returns>实例对象</returns> /// <remarks>运用晚绑定方式动态创建一个实例</remarks> public static object CreateSerializableInstance(System.Type type, params object[] constructorParams) { type.IsSerializable.FalseThrow <SerializationException>("XElementFormatter错误,类型{0}不可序列化", type.AssemblyQualifiedName); return(TypeCreator.CreateInstance(type, constructorParams)); }