Read an object variable having “FileInfo” object content in C# script task

廉价感情. 提交于 2021-02-11 14:53:41

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!