问题
I'm trying to loop and read an object variable(as shown in the pic) where it has the file details(name/size/datemodified) from a share point location.
Basically I need something similar to below code to just read the "Name" value.
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;
}
I'm not sure how to get the "Name" field value added to the list. Any help is much appreciated.
回答1:
So this is a type where we have objects containing objects values. So to access this please use the below method.
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;
}
来源:https://stackoverflow.com/questions/61945475/read-an-object-variable-having-fileinfo-object-content-in-c-sharp-script-task