如何在SSIS脚本任务中传递自定义对象列表? [英] How to pass a List of custom objects in a SSIS Script Task?

查看:86
本文介绍了如何在SSIS脚本任务中传递自定义对象列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本任务,该任务创建一个自定义对象列表并将其设置为SSIS对象变量.

I have a Script Task that creates a list of custom objects and sets them to an SSIS object variable.

自定义类:

public class Dog
{
    public string Name { get; set; }
}

用于填充列表并将其设置为SSIS对象变量"myDogs"的代码:

Code to populate the List and set to an SSIS object variable "myDogs":

public void Main()
{
    List<Dog> dogs = new List<Dog>();

    Dog dog1 = new Dog();
    dog1.Name = "Fido";

    Dog dog2 = new Dog();
    dog1.Name = "Roofus";

    dogs.Add(dog1);
    dogs.Add(dog2);

    Dts.Variables["myDogs"].Value = dogs;
}

在第二个脚本任务中,我试图将我的"myDogs"对象变量读回到列表中:

In a second Script Task, I am trying to read my "myDogs" object variable back into a List:

在第二个脚本任务中复制的自定义类:

Custom class copied over in the second Script Task:

public class Dog
{
    public string Name { get; set; }
}

第二个脚本任务中的主要代码:

Main code in my second Script Task:

public void Main()
{
    var varDogs = Dts.Variables["myDogs"].Value;
    List<Dog> dogs = new List<Dog>();

    dogs = (List<Dog>)varDogs;
}

我的varDogs对象正确地从我的SSIS对象变量"myDogs"中加载了数据.但是,当我尝试将varDogs强制转换为Dog类型的列表时,收到一条错误消息,提示无法强制转换System.Collections.Generic.List类型的对象".

My varDogs object correctly loads the data from my SSIS object variable "myDogs". However, when I try to cast varDogs to a List of type Dog I receive an error message saying "Unable to cast object of type System.Collections.Generic.List".

有人知道我将如何将这些var数据重新投射到列表中吗?谢谢.

Does anyone know how I would be able to cast this var data back into a List? Thanks.

推荐答案

在尝试任何其他操作之前,请将自定义类编译为.dll,然后将其安装到GAC(全局程序集缓存)中.当与自定义对象进行交互时,SSIS确实很挑剔,并且希望在那里找到对它们的引用.可以在此处.安装.dll后,只需在脚本任务中引用它即可,SSIS会自动搜索GAC签名.

Before you try anything else, compile your custom classes to a .dll and install it to your GAC (Global Assembly Cache). SSIS is really picky when it comes to interacting with custom objects and it expects to find references to them there. The MSDN example of GAC installation can be found here. Once you install the .dll just reference it in your script task and SSIS will search for the GAC signature automatically.

这是在SSIS中管理自定义对象的正确方法,因为您在两个脚本任务中都引用了一个类.尽管您当前的两个脚本任务之间的类相同,但是您的错误表明它们来自单独的命名空间.

This is the proper way of managing custom objects in SSIS because you are referencing one class in both script tasks. Although the class is identical between your two current script tasks, your error is indicating that they are from separate namespaces.

正如其他人所建议的那样,序列化是可行的,但这至少是一个棘手的解决方案,因为您必须在两面都维护类(脚本任务1和脚本任务2).在开始使用复杂类型之前,最好先熟悉本机SSIS解决方案:)

As others have suggested, serialization would work, but it is a hackish solution at the very least because you would have to maintain the class on both sides (Script Task 1 & Script Task 2). Better to familiarize yourself with the native SSIS solution before you start working with complex types :)

这篇关于如何在SSIS脚本任务中传递自定义对象列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆