在 C# 脚本任务中读取具有 "FileInfo" object 内容的 object 变量
Read an object variable having "FileInfo" object content in C# script task
我正在尝试循环并读取一个 object 变量(如图所示),其中包含来自共享点位置的文件详细信息 (name/size/datemodified)。
基本上我需要类似于下面代码的东西来读取 "Name" 值。
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;
using System.Collections;
using System.Collections.Generic;
public void Main()
{
var fileinfo = Dts.Variables["User::DvarObj"].Value;
Dts.Variables["User::Local"].Value = new List<String>();
List<String> OutputFileNames = (List<String>)Dts.Variables["User::Local"].Value;
var collection = fileinfo as IEnumerable;
if (collection != null)
{
foreach (var item in collection)
{
OutputFileNames.Add(item.GetType());
}
}
var id = fileinfo.GetType().Name;
Dts.TaskResult = (int)ScriptResults.Success;
}
我不确定如何将 "Name" 字段值添加到列表中。任何帮助深表感谢。
所以这是一种我们有包含对象值的对象的类型。因此,要访问它,请使用以下方法。
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;
using System.Collections;
using System.Xml;
using System.Collections.Generic;
using System.Linq;
public void Main()
{
var fileinfo = Dts.Variables["User::DvarObj"].Value;
Dts.Variables["User::Local"].Value = new List<String>();
List<String> OutputFileNames = (List<String>)Dts.Variables["User::Local"].Value;
string outputresultnames = "";
foreach (object element in (fileinfo as IEnumerable ?? Enumerable.Empty<object>()))
{
var type = element.GetType();
var props = type.GetProperties();
object result = props.First().GetValue(element, null);
outputresultnames = (string)result;
OutputFileNames.Add(outputresultnames);
}
Dts.Variables["User::Local"].Value = OutputFileNames;
Dts.TaskResult = (int)ScriptResults.Success;
}
我正在尝试循环并读取一个 object 变量(如图所示),其中包含来自共享点位置的文件详细信息 (name/size/datemodified)。
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;
using System.Collections;
using System.Collections.Generic;
public void Main()
{
var fileinfo = Dts.Variables["User::DvarObj"].Value;
Dts.Variables["User::Local"].Value = new List<String>();
List<String> OutputFileNames = (List<String>)Dts.Variables["User::Local"].Value;
var collection = fileinfo as IEnumerable;
if (collection != null)
{
foreach (var item in collection)
{
OutputFileNames.Add(item.GetType());
}
}
var id = fileinfo.GetType().Name;
Dts.TaskResult = (int)ScriptResults.Success;
}
我不确定如何将 "Name" 字段值添加到列表中。任何帮助深表感谢。
所以这是一种我们有包含对象值的对象的类型。因此,要访问它,请使用以下方法。
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;
using System.Collections;
using System.Xml;
using System.Collections.Generic;
using System.Linq;
public void Main()
{
var fileinfo = Dts.Variables["User::DvarObj"].Value;
Dts.Variables["User::Local"].Value = new List<String>();
List<String> OutputFileNames = (List<String>)Dts.Variables["User::Local"].Value;
string outputresultnames = "";
foreach (object element in (fileinfo as IEnumerable ?? Enumerable.Empty<object>()))
{
var type = element.GetType();
var props = type.GetProperties();
object result = props.First().GetValue(element, null);
outputresultnames = (string)result;
OutputFileNames.Add(outputresultnames);
}
Dts.Variables["User::Local"].Value = OutputFileNames;
Dts.TaskResult = (int)ScriptResults.Success;
}