对象以列出C#和SSI

对象以列出C#和SSI,c#,list,generics,ssis,C#,List,Generics,Ssis,背景: 有点奇怪。使用SSI处理来自API的数据,我的第一个脚本获取数据并将其放在一个名为“TestModel”的强类型模型上 然后,SSIS包将生成的信息保存到一个变量中,然后将该变量传递到SSIS包的下一部分。由于SSI的名称空间不存在,因此每个脚本任务都是独立的 脚本任务一命名空间:ST_a048e0de86e1432d8da6f60b5d7055db 脚本任务二命名空间:SC_0573a66ec6c0486a98ee00cea4365654 问题是: 第二个SSIS脚本任务拾取变量并读取

背景:

有点奇怪。使用SSI处理来自API的数据,我的第一个脚本获取数据并将其放在一个名为“TestModel”的强类型模型上

然后,SSIS包将生成的信息保存到一个变量中,然后将该变量传递到SSIS包的下一部分。由于SSI的名称空间不存在,因此每个脚本任务都是独立的

脚本任务一命名空间:ST_a048e0de86e1432d8da6f60b5d7055db

脚本任务二命名空间:SC_0573a66ec6c0486a98ee00cea4365654

问题是:

第二个SSIS脚本任务拾取变量并读取它,我可以在调试中看到我的所有行和所有相关数据。现在事情开始变得奇怪,当它到达我的第二个脚本时,列表的类型

object {System.Collections.Generic.List<ST_a048e0de86e1432d8da6f60b5d7055db.TestModel>}
我可以用这个列表做什么?几乎没什么。。。 我已经将TestModel.cs从脚本名称空间1复制到了名称空间2,希望如果我创建了一个新列表并将对象也传递给它,它会很好地匹配,唉,不

到目前为止,我所尝试的:

IEnumerable e=数据作为列表//返回0行
IEnumerable列表=(IEnumerable)数据;//返回所有行,键入System.Collections.IEnumerable{System.Collections.Generic.List}
List listtest=((TestModel[])数据.ToList()//运行时错误
List listtest2=List.Cast().ToList()//运行时错误-无法将类型“ST_a048e0de86e1432d8da6f60b5d7055db.TestModel”强制转换为类型“SC_0573a66ec6c0486a98ee00cea4365654.TestModel”
我的最终目标是,我需要一些可以循环并操作到SSIS可以消化的对象中的东西。这部分很容易,但要让它循环起来却非常困难

额外注意:SSIS软件包对依赖关系非常恼火,因此非常希望避免使用任何特殊的东西。此外,名称空间与另一个名称空间100%隔离,两者之间不可能通信

尝试使用

试用

您可以尝试使用将TestModel类映射到彼此。它易于安装和使用

这是我的想法,所以可能需要调整,但代码看起来非常类似:

var config = new MapperConfiguration(cfg => {
    CreateMap<ST_a048e0de86e1432d8da6f60b5d7055db.TestModel
              , SC_0573a66ec6c0486a98ee00cea4365654.TestModel>();
    cfg.AddProfile<testModel>();
});

var mapper = config.CreateMapper();    
编辑:尝试2

您说过您尝试将
TestModel
类移动到第二个名称空间。如果没有,您可能需要将其添加到或类似的内容中。那么它可能很简单,只要尝试一下:

var listTest = data.Select(x => new TestModel 
                       { property1 = x.property1,
                         property2 = x.property2
                         //etc...
                         }).ToList();
这将为您提供一个新的
列表

,您可以尝试使用它将TestModel类相互映射。它易于安装和使用

这是我的想法,所以可能需要调整,但代码看起来非常类似:

var config = new MapperConfiguration(cfg => {
    CreateMap<ST_a048e0de86e1432d8da6f60b5d7055db.TestModel
              , SC_0573a66ec6c0486a98ee00cea4365654.TestModel>();
    cfg.AddProfile<testModel>();
});

var mapper = config.CreateMapper();    
编辑:尝试2

您说过您尝试将
TestModel
类移动到第二个名称空间。如果没有,您可能需要将其添加到或类似的内容中。那么它可能很简单,只要尝试一下:

var listTest = data.Select(x => new TestModel 
                       { property1 = x.property1,
                         property2 = x.property2
                         //etc...
                         }).ToList();

这应该会给你一个新的
列表

我想详细介绍一下John Ephraim Tugado的解决方案,因为它看起来有点不完整

public void Main()
{
    try
    {
        List<TestModel> testModelList = BuildObjectList();

        TestModel testModel = new TestModel();

        testModel.prop1 = "new prop";
        testModel.prop2 = true;

        testModelList.Add(testModel);

        //Now that we have a testModelList in the correct local class we can 
        //modify it and load it back into the globally held variable for 
        //another Component to use

        Dts.Variables["User::myObjectList"].Value = testModelList;

        Dts.TaskResult = (int)ScriptResults.Success;
    }
    catch
    {
        Dts.TaskResult = (int)ScriptResults.Failure;
    }
}

private List<TestModel> BuildObjectList()
{
    try
    {
        List<TestModel> RunningList = new List<TestModel>();
        TestModel localModel = new TestModel();

        var data = Dts.Variables["User::myObjectList"].Value;
        IEnumerable enumDataList = (IEnumerable)data;

        foreach (var currentObj in enumDataList)
        {
            localModel = GetSingleResult(currentObj);
            RunningList.Add(localModel);
            localModel = new TestModel();
        }
        return RunningList;
    }
    catch
    {
        return new List<TestModel>();
    }
}

private TestModel GetSingleResult(object currentObj)
{
    try
    {
        TestModel returnedResult = new TestModel();

        PropertyInfo[] properties = currentObj.GetType().GetProperties();
        foreach (PropertyInfo pi in properties)
        {
            switch (pi.Name)
            {
                case "prop1":
                    returnedResult.prop1 = pi.GetValue(currentObj, null).ToString();
                    break;
                case "prop2":
                    returnedResult.prop2 = Convert.ToBoolean(pi.GetValue(currentObj, null));
                    break;
                default:
                    break;
            }
        }

        return returnedResult;
    }
    catch {
        return new TestModel();
    }
}

internal class TestModel
{
    internal string prop1 { get; set; }
    internal bool prop2 { get; set; }
}
public void Main()
{
尝试
{
List testModelList=BuildObjectList();
TestModel TestModel=新的TestModel();
testModel.prop1=“新道具”;
testModel.prop2=true;
添加(testModel);
//现在我们在正确的本地类中有了testModelList,我们可以
//修改它并将其加载回全局保持变量中,以便
//要使用的另一个组件
变量[“User::myObjectList”]。值=testModelList;
Dts.TaskResult=(int)ScriptResults.Success;
}
抓住
{
Dts.TaskResult=(int)ScriptResults.Failure;
}
}
私有列表BuildObjectList()
{
尝试
{
List RunningList=新列表();
TestModel localModel=新的TestModel();
var data=Dts.Variables[“User::myObjectList”].Value;
IEnumerable enumDataList=(IEnumerable)数据;
foreach(enumDataList中的var currentObj)
{
localModel=GetSingleResult(currentObj);
添加(localModel);
localModel=newtestmodel();
}
返回运行列表;
}
抓住
{
返回新列表();
}
}
私有测试模型GetSingleResult(对象currentObj)
{
尝试
{
TestModel returnedResult=新的TestModel();
PropertyInfo[]properties=currentObj.GetType().GetProperties();
foreach(PropertyInfo pi in properties)
{
开关(pi.Name)
{
案例“prop1”:
returnedResult.prop1=pi.GetValue(currentObj,null).ToString();
打破
案例“prop2”:
returnedResult.prop2=Convert.ToBoolean(pi.GetValue(currentObj,null));
打破
违约:
打破
}
}
返回返回结果;
}
抓住{
返回新的TestModel();
}
}
内部类测试模型
{
内部字符串prop1{get;set;}
内部boolprop2{get;set;}
}

我想详细介绍一下约翰·以法莲·图加多的解决方案,因为它似乎有点不完整

public void Main()
{
    try
    {
        List<TestModel> testModelList = BuildObjectList();

        TestModel testModel = new TestModel();

        testModel.prop1 = "new prop";
        testModel.prop2 = true;

        testModelList.Add(testModel);

        //Now that we have a testModelList in the correct local class we can 
        //modify it and load it back into the globally held variable for 
        //another Component to use

        Dts.Variables["User::myObjectList"].Value = testModelList;

        Dts.TaskResult = (int)ScriptResults.Success;
    }
    catch
    {
        Dts.TaskResult = (int)ScriptResults.Failure;
    }
}

private List<TestModel> BuildObjectList()
{
    try
    {
        List<TestModel> RunningList = new List<TestModel>();
        TestModel localModel = new TestModel();

        var data = Dts.Variables["User::myObjectList"].Value;
        IEnumerable enumDataList = (IEnumerable)data;

        foreach (var currentObj in enumDataList)
        {
            localModel = GetSingleResult(currentObj);
            RunningList.Add(localModel);
            localModel = new TestModel();
        }
        return RunningList;
    }
    catch
    {
        return new List<TestModel>();
    }
}

private TestModel GetSingleResult(object currentObj)
{
    try
    {
        TestModel returnedResult = new TestModel();

        PropertyInfo[] properties = currentObj.GetType().GetProperties();
        foreach (PropertyInfo pi in properties)
        {
            switch (pi.Name)
            {
                case "prop1":
                    returnedResult.prop1 = pi.GetValue(currentObj, null).ToString();
                    break;
                case "prop2":
                    returnedResult.prop2 = Convert.ToBoolean(pi.GetValue(currentObj, null));
                    break;
                default:
                    break;
            }
        }

        return returnedResult;
    }
    catch {
        return new TestModel();
    }
}

internal class TestModel
{
    internal string prop1 { get; set; }
    internal bool prop2 { get; set; }
}
public void Main()
{
尝试
{
List testModelList=BuildObjectList();
TestModel TestModel=新的TestModel();
testModel.prop1=“新道具”;
testModel.prop2=true;
添加(testModel);
//现在我们在正确的本地类中有了testModelList,我们可以
//修改它并将其加载回全局保持变量中,以便
//要使用的另一个组件
变量[“User::myObjectList”]。值=testModelList;
Dts.TaskResult=(int)ScriptResults.Success;
}
抓住
{
Dts.TaskResult=(int)ScriptResults.Failure;
}
}
私有列表BuildObjectList()
{
尝试
{
List RunningList=新列表();
TestModel localModel=新的TestModel(
var listTest = data.Select(x => new TestModel 
                       { property1 = x.property1,
                         property2 = x.property2
                         //etc...
                         }).ToList();
public void Main()
{
    try
    {
        List<TestModel> testModelList = BuildObjectList();

        TestModel testModel = new TestModel();

        testModel.prop1 = "new prop";
        testModel.prop2 = true;

        testModelList.Add(testModel);

        //Now that we have a testModelList in the correct local class we can 
        //modify it and load it back into the globally held variable for 
        //another Component to use

        Dts.Variables["User::myObjectList"].Value = testModelList;

        Dts.TaskResult = (int)ScriptResults.Success;
    }
    catch
    {
        Dts.TaskResult = (int)ScriptResults.Failure;
    }
}

private List<TestModel> BuildObjectList()
{
    try
    {
        List<TestModel> RunningList = new List<TestModel>();
        TestModel localModel = new TestModel();

        var data = Dts.Variables["User::myObjectList"].Value;
        IEnumerable enumDataList = (IEnumerable)data;

        foreach (var currentObj in enumDataList)
        {
            localModel = GetSingleResult(currentObj);
            RunningList.Add(localModel);
            localModel = new TestModel();
        }
        return RunningList;
    }
    catch
    {
        return new List<TestModel>();
    }
}

private TestModel GetSingleResult(object currentObj)
{
    try
    {
        TestModel returnedResult = new TestModel();

        PropertyInfo[] properties = currentObj.GetType().GetProperties();
        foreach (PropertyInfo pi in properties)
        {
            switch (pi.Name)
            {
                case "prop1":
                    returnedResult.prop1 = pi.GetValue(currentObj, null).ToString();
                    break;
                case "prop2":
                    returnedResult.prop2 = Convert.ToBoolean(pi.GetValue(currentObj, null));
                    break;
                default:
                    break;
            }
        }

        return returnedResult;
    }
    catch {
        return new TestModel();
    }
}

internal class TestModel
{
    internal string prop1 { get; set; }
    internal bool prop2 { get; set; }
}