private static object CreateSerializableInstance(string typeDescription, params object[] constructorParams) { Type type = TypeCreator.GetTypeInfo(typeDescription); ExceptionHelper.FalseThrow <TypeLoadException>(type != null, Resource.TypeLoadException, typeDescription); return(CreateSerializableInstance(type, constructorParams)); }
/// <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)); }
private void DeserializeNodesToProperties(XElement parent, object graph, XmlDeserializeContext context) { TypeFields tf = TypeFields.GetTypeFields(graph.GetType()); foreach (KeyValuePair <TypeFieldInfo, ExtendedFieldInfo> kp in tf.Fields) { ExtendedFieldInfo efi = kp.Value; if (efi.IsNotSerialized == false && OnFieldCanXElementSerialize(efi)) { System.Type realType = efi.FieldInfo.FieldType; var propertiesElement = from property in parent.Descendants("F") where (property.AttributeWithAlterName("name", "n", string.Empty) == efi.AlternateFieldName || property.AttributeWithAlterName("name", "n", string.Empty) == efi.FieldInfo.Name) && context.TypeContext[property.AttributeWithAlterName("ownerTypeID", "oti", -1)] == kp.Key.ObjectType select property; if (propertiesElement.FirstOrDefault() == null) { propertiesElement = from property in parent.Descendants("Field") where (property.AttributeWithAlterName("name", "n", string.Empty) == efi.AlternateFieldName || property.AttributeWithAlterName("name", "n", string.Empty) == efi.FieldInfo.Name) && context.TypeContext[property.AttributeWithAlterName("ownerTypeID", "oti", -1)] == kp.Key.ObjectType select property; } XElement propertyElement = propertiesElement.FirstOrDefault(); if (propertyElement != null) { object data = null; if (propertyElement.AttributeWithAlterName("isRef", "r", false) == true) { XElement objectElement = GetObjectElementByID(context.RootElement, propertyElement.AttributeWithAlterName("value", "v", 0)); if (objectElement != null) { bool ignoreError = propertyElement.Attribute("ide", false); if (ignoreError == false) { XElementFieldSerializeAttribute attr = AttributeHelper.GetCustomAttribute <XElementFieldSerializeAttribute>(efi.FieldInfo); if (attr != null) { ignoreError = attr.IgnoreDeserializeError; } } data = DeserializeNodeToObject(objectElement, ignoreError, context); SetValueToObject(efi.FieldInfo, graph, data); } } else { data = propertyElement.AttributeWithAlterName("value", "v", TypeCreator.GetTypeDefaultValue(realType)); if (Convertible(realType, data)) { SetValueToObject(efi.FieldInfo, graph, ConvertData(efi.FieldInfo, data)); } } } } } DeserializeXmlSerilizableList(parent, graph, context); }