using System;
using System.Reflection;
using System.Collections.Generic;
using Common.TextUtil;
using Common.Exceptions;
namespace NSC
{
///
/// 功能:公共参数结构
/// 作者:王昊昇
/// 时间:2012.11.8
///
public class ParameterStruct
{
///
/// 创建ParameterStruct类的新实例
///
public ParameterStruct()
{
}
///
/// 使用给定的属性创建ParameterStruct类的新实例
///
/// 数据,支持格式:string、所有值类型数据、属性全部是值类型的实体及List列表、DataTable、byte[]、string[]
public ParameterStruct(object data)
{
this.Data = data;
}
///
/// 数据格式
///
internal string DataFormat { get; set; }
///
/// 数据,支持格式:string、所有值类型数据、属性全部是值类型的实体及List列表、DataTable、byte[]、string[]
///
internal object Data { get; set; }
///
/// 参数链接
///
internal string Link { get; set; }
///
/// 获取List类型的数据
///
/// 转换的目标类型
/// 返回实体列表数据
public List GetList() where T : new()
{
if (Data is List)
{
return Data as List;
}
else
{
List list = null;
Type t = typeof(T);
if (t.IsValueType || t == typeof(string))
{
list = GetValueTypeInstanceList();
}
else
{
list = GetReferenceInstanceList();
}
Data = list;
return list;
}
}
///
/// 获取引用类型泛型列表
///
/// 引用型实体的类型
/// 返回引用型泛型数据
private List GetReferenceInstanceList() where T : new()
{
System.Xml.XmlNode itmHead = ((System.Xml.XmlNode)Data).SelectSingleNode("Items");
System.Xml.XmlNodeList itmList = itmHead.SelectNodes("Item");
List list = new List();
foreach (System.Xml.XmlNode n in itmList)
{
T t = new T();
PropertyInfo[] publicProperties = t.GetType().GetProperties();
for (int i = 0; i < publicProperties.Length; i++)
{
publicProperties[i].SetValue(t, Common.AssemblyUtil.TypeFactory.ConvertValueTypeObjectToOriginalType(n.Attributes[publicProperties[i].Name].Value, publicProperties[i].PropertyType), null);
}
list.Add(t);
}
return list;
}
///
/// 获取值类型泛型列表
///
/// 值类型实体的类型
/// 返回值类型泛型数据
private List GetValueTypeInstanceList() where T : new()
{
System.Xml.XmlNode itmHead = ((System.Xml.XmlNode)Data).SelectSingleNode("Items");
System.Xml.XmlNodeList itmList = itmHead.SelectNodes("Item");
List list = new List();
Type t = typeof(T);
//此代码可替换下面代码,性能略逊
//foreach (System.Xml.XmlNode n in itmList)
//{
// list.Add((T)Common.AssemblyUtil.TypeFactory.ConvertValueTypeObjectToOriginalType(n.Attributes["Value"].Value, t));
//}
if (t.IsEnum)
{
foreach (System.Xml.XmlNode n in itmList)
{
list.Add((T)Common.AssemblyUtil.TypeFactory.ConvertToEnum(n.Attributes["Value"].Value, t));
}
}
else if (t == typeof(string))
{
foreach (System.Xml.XmlNode n in itmList)
{
list.Add((T)Common.AssemblyUtil.TypeFactory.ConvertToString(n.Attributes["Value"].Value));
}
}
else if (t == typeof(int))
{
foreach (System.Xml.XmlNode n in itmList)
{
list.Add((T)Common.AssemblyUtil.TypeFactory.ConvertToInt32(n.Attributes["Value"].Value));
}
}
else if (t == typeof(short))
{
foreach (System.Xml.XmlNode n in itmList)
{
list.Add((T)Common.AssemblyUtil.TypeFactory.ConvertToInt16(n.Attributes["Value"].Value));
}
}
else if (t == typeof(long))
{
foreach (System.Xml.XmlNode n in itmList)
{
list.Add((T)Common.AssemblyUtil.TypeFactory.ConvertToInt64(n.Attributes["Value"].Value));
}
}
else if (t == typeof(decimal))
{
foreach (System.Xml.XmlNode n in itmList)
{
list.Add((T)Common.AssemblyUtil.TypeFactory.ConvertToDecimal(n.Attributes["Value"].Value));
}
}
else if (t == typeof(Single))
{
foreach (System.Xml.XmlNode n in itmList)
{
list.Add((T)Common.AssemblyUtil.TypeFactory.ConvertToSingle(n.Attributes["Value"].Value));
}
}
else if (t == typeof(double))
{
foreach (System.Xml.XmlNode n in itmList)
{
list.Add((T)Common.AssemblyUtil.TypeFactory.ConvertToDouble(n.Attributes["Value"].Value));
}
}
else if (t == typeof(bool))
{
foreach (System.Xml.XmlNode n in itmList)
{
list.Add((T)Common.AssemblyUtil.TypeFactory.ConvertToBoolean(n.Attributes["Value"].Value));
}
}
else
{
throw new Common.Exceptions.NotSupportedFormatException(t);
}
return list;
}
///
/// 获取实体类型的数据
///
/// 实体的数据类型
/// 返回实体引用
public T GetEntity() where T : new()
{
if (Data.GetType() == typeof(T))
{
return (T)Data;
}
else
{
System.Xml.XmlNode itmHead = ((System.Xml.XmlNode)Data).SelectSingleNode("Items");
System.Xml.XmlNode itm = itmHead.SelectSingleNode("Item");
T t = new T();
PropertyInfo[] publicProperties = t.GetType().GetProperties();
for (int i = 0; i < publicProperties.Length; i++)
{
// 修改: 适应实体属性也是一个类 李鹏飞 2013.05.21 ---开始
if (itm.Attributes[publicProperties[i].Name] != null &&
(t.GetType().GetProperty(publicProperties[i].Name).PropertyType.IsValueType ||
t.GetType().GetProperty(publicProperties[i].Name).PropertyType.FullName.Contains("String")))
{
publicProperties[i].SetValue(t, Common.AssemblyUtil.TypeFactory.ConvertValueTypeObjectToOriginalType(itm.Attributes[publicProperties[i].Name].Value, publicProperties[i].PropertyType), null);
}
// 修改: 适应实体属性也是一个类 李鹏飞 2013.05.21 ---结束
}
return t;
}
}
///
/// 获取String类型的数据
///
/// 返回字符串
public string GetString()
{
return Data.ToString();
}
///
/// 获取Short类型的数据
///
/// 返回16位整型数据
public short GetShort()
{
return Data.ToString().ToInt16();
}
///
/// 获取Int类型的数据
///
/// 返回32位整形数据
public int GetInteger()
{
return Data.ToString().ToInt32();
}
///
/// 获取Long类型的数据
///
/// 返回64位整型数据
public long GetLong()
{
return Data.ToString().ToInt64();
}
///
/// 获取Single类型的数据
///
/// 返回单精度浮点数据
public Single GetSingle()
{
return Data.ToString().ToSingle();
}
///
/// 获取Double类型的数据
///
/// 返回双精度浮点数据
public Double GetDouble()
{
return Data.ToString().ToDouble();
}
///
/// 获取Decimal类型的数据
///
/// 返回高精度浮点数据
public decimal GetDecimal()
{
return Data.ToString().ToDecimal();
}
///
/// 获取数据表
///
/// 返回数据表
public System.Data.DataTable GetDataTable()
{
return Data as System.Data.DataTable;
}
///
/// 获取二进制数组
///
/// 返回二进制数组
public byte[] GetBinary()
{
return Data as byte[];
}
///
/// 获取字符串数组
///
/// 返回字符串数组
public string[] GetArray()
{
return Data as string[];
}
///
/// 设置数据的值
///
/// 数据,支持格式:string、所有值类型数据、属性全部是值类型或string的实体及List列表、DataTable、byte[]
public void SetData(object data)
{
this.Data = data;
}
}
}