1, 原代码:
#region ### ScriptTask List<PkgScriptTask> lstPkgScriptTask = pkgIndex.LstPkgScriptTask; if (lstPkgScriptTask != null && lstPkgScriptTask.Count > 0) { txtPkgInfo.AppendText("\r\n" + lstPkgScriptTask[0].GetType().ToString() + ":"); txtPkgInfo.AppendText("\r\nSTART analysizing---"); txtPkgInfo.AppendText("\r\n"); foreach (PkgScriptTask pbi in lstPkgScriptTask) // list title { foreach (System.Reflection.PropertyInfo info in typeof(PkgScriptTask).GetProperties()) { txtPkgInfo.AppendText(Convert.ToChar(Keys.Tab) + info.Name); } break; } txtPkgInfo.AppendText("\r\n"); foreach (PkgScriptTask pbi in lstPkgScriptTask) // list value { foreach (System.Reflection.PropertyInfo info in typeof(PkgScriptTask).GetProperties()) { txtPkgInfo.AppendText(Convert.ToChar(Keys.Tab) + info.GetValue(pbi, null).ToString()); } txtPkgInfo.AppendText("\r\n\r\n"); } txtPkgInfo.AppendText("END analysizing---\r\n"); } //OutputClassObj(lstPkgScriptTask,pkgIndex); #endregion ### ScriptTask
2,通过泛型改进后的代码及调用示例
private void OutputClassObj<T>(List<T> t, PkgIndex pkgIndex) { List<T> lstPkgObj = new List<T>(); lstPkgObj = t; if (lstPkgObj != null && lstPkgObj.Count > 0) { txtPkgInfo.AppendText("\r\n" + lstPkgObj[0].GetType().ToString() + ":"); txtPkgInfo.AppendText("\r\nSTART analysizing---"); txtPkgInfo.AppendText("\r\n"); foreach (T pbi in lstPkgObj) // list title { foreach (System.Reflection.PropertyInfo info in typeof(T).GetProperties()) { txtPkgInfo.AppendText(Convert.ToChar(Keys.Tab) + info.Name); } break; } txtPkgInfo.AppendText("\r\n"); foreach (T pbi in lstPkgObj) // list value { foreach (System.Reflection.PropertyInfo info in typeof(T).GetProperties()) { txtPkgInfo.AppendText(Convert.ToChar(Keys.Tab) + info.GetValue(pbi, null).ToString()); } txtPkgInfo.AppendText("\r\n"); } txtPkgInfo.AppendText("END analysizing---\r\n"); } }
调用示例:
private void OpenPkgIndex(PkgIndex pkgIndex) { #region ### basic Info List<PkgBasicInfo> lstPkgBasicInfo = pkgIndex.LstPkgBasicInfo; OutputClassObj(lstPkgBasicInfo, pkgIndex); #endregion ### basic info #region ### configuration List<PkgConfiguration> lstPkgConfiguration = pkgIndex.LstPkgConfig; OutputClassObj(lstPkgConfiguration, pkgIndex); #endregion ### configuration #region ### Connection List<PkgConnection> lstPkgConnection = pkgIndex.LstPkgConnection; OutputClassObj(lstPkgConnection, pkgIndex); #endregion ### Connection #region ### Variable List<PkgVariable> lstPkgVariable = pkgIndex.LstPkgVariable; OutputClassObj(lstPkgVariable, pkgIndex); #endregion ### Variable #region ### InputColumn List<PkgInputColumn> lstPkgInputColumn = pkgIndex.LstPkgInputColumn; OutputClassObj(lstPkgInputColumn, pkgIndex); #endregion ### InputColumn #region ### OutputColumn List<PkgOutputColumn> lstPkgOutputColumn = pkgIndex.LstPkgOutputColumn; OutputClassObj(lstPkgOutputColumn, pkgIndex); #endregion ### OutputColumn #region ### SqlScript List<PkgSqlScript> lstPkgSqlScript = pkgIndex.LstPkgSqlScript; OutputClassObj(lstPkgSqlScript, pkgIndex); #endregion ### ScriptTask #region ### ScriptTask List<PkgScriptTask> lstPkgScriptTask = pkgIndex.LstPkgScriptTask; OutputClassObj(lstPkgScriptTask, pkgIndex); #endregion ### ScriptTask }
3,上述用到的几个类:(其实这几个类是在解析SSIS包时用到的)
PkgIndex.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ReadPkgLib { public class PkgIndex { public List<PkgBasicInfo> LstPkgBasicInfo { get; set; } public List<PkgConfiguration> LstPkgConfig { get; set; } public List<PkgConnection> LstPkgConnection { get; set; } public List<PkgVariable> LstPkgVariable { get; set; } public List<PkgScriptTask> LstPkgScriptTask { get; set; } public List<PkgSqlScript> LstPkgSqlScript { get; set; } public List<PkgInputColumn> LstPkgInputColumn { get; set; } public List<PkgOutputColumn> LstPkgOutputColumn { get; set; } public void ClearAll() { if (LstPkgBasicInfo == null || LstPkgBasicInfo.Count > 0) LstPkgBasicInfo = new List<PkgBasicInfo>(); if (LstPkgConfig == null || LstPkgConfig.Count > 0) LstPkgConfig = new List<PkgConfiguration>(); if (LstPkgConnection == null || LstPkgConnection.Count > 0) LstPkgConnection = new List<PkgConnection>(); if (LstPkgVariable == null || LstPkgVariable.Count > 0) LstPkgVariable = new List<PkgVariable>(); if (LstPkgScriptTask == null || LstPkgScriptTask.Count > 0) LstPkgScriptTask = new List<PkgScriptTask>(); if (LstPkgSqlScript == null || LstPkgSqlScript.Count > 0) LstPkgSqlScript = new List<PkgSqlScript>(); if (LstPkgInputColumn == null || LstPkgInputColumn.Count > 0) LstPkgInputColumn = new List<PkgInputColumn>(); if (LstPkgOutputColumn == null || LstPkgOutputColumn.Count > 0) LstPkgOutputColumn = new List<PkgOutputColumn>(); } public PkgIndex() { ClearAll(); } } }
PkgBasicInfo.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ReadPkgLib { public class PkgBasicInfo:PkgObj { public bool HasFileMetrics { get; set; } public bool HasLogging { get; set; } public string CreatorName { get; set; } public string CreatorComputer { get; set; } public string CreationDate { get; set; } public string LocalSvnUrl { get; set; } public string ServerSvnUrl { get; set; } } }
PkgConfiguration.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ReadPkgLib { public class PkgConfiguration : PkgObj { public string ConfigurationString { get; set; } public string ConfigurationVariable { get; set; } } }
PkgConnection.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ReadPkgLib { public class PkgConnection : PkgObj { public string CreationName { get; set; } public string PropertyExpression { get; set; } public string EvaluateValue { get; set; } } }
PkgInputColumn.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ReadPkgLib { public class PkgInputColumn : PkgObj { public int RowNum { get; set; } public int Length { get; set; } public string DataType { get; set; } } }
PkgOutputColumn.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ReadPkgLib { public class PkgOutputColumn : PkgObj { public int RowNum { get; set; } public int Length { get; set; } public string DataType { get; set; } } }
PkgScriptTask.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ReadPkgLib { public class PkgScriptTask : PkgObj { public int RowNum { get; set; } public string Language { get; set; } public string EntryPoint { get; set; } public bool SaveBinaries { get; set; } public string ReadOnlyVariables { get; set; } public string ReadWriteVariables { get; set; } public bool OptimizeScriptExecution { get; set; } public string Script { get; set; } } }
PkgSqlScript.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ReadPkgLib { public class PkgSqlScript : PkgObj { public int RowNum { get; set; } public string Script { get; set; } } }
PkgVariable.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ReadPkgLib { public class PkgVariable : PkgObj { public int ReadOnly { get; set; } public string Namespace { get; set; } public string VariableValue { get; set; } } }
PkgObj.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ReadPkgLib { public class PkgObj { public string ObjectName { get; set; } } }