0

I have a bit of a weird issue. Working in C# script with SSIS I have developed a need to build a List based off Dynamic Data.

Background

To explain it, a script task is fired that has a variable API URL, this goes off and pulls a JSON string back and then throws it into a strongly typed list using the following code.

 var listobject = get_APIData<ApplicationOneDataSet>(url)

The class that does this is long winded and not really needed in the context of this issue. ApplicationOneDataSet is a strongly typed match to one of the possible JSON results returned by get_APIData.

Now I have a need to change ApplicationOneDataSet to ApplicationTwoDataSet dynamically based on which API URL I pass to the script.

So what I have done is send through a second variable to the script called class name which contains the string "ApplicationDataSetOne" or "ApplicationDataSetTwo" based on which context I call it under.

The Question

My question is how can I dynamically vary this line:

var listobject = get_APIData<ApplicationOneDataSet>(url)

With the string variable passed into the script.

My original thinking was something along the lines of this:

 var ClassType = (string) Dts.Variables["AppClassName"].Value;
 Type type = Type.GetType(ClassType);
 var listobject = get_APIData<type>(url)

Though it doesn't seem to like that. Any tips would be great!

Caz1224
  • 1,539
  • 1
  • 14
  • 37

2 Answers2

0

I'm not sure I fully understood what you are trying to do, but how about writing an interface ApplicationDataSet and then making a list of it? This way your list is going to be able to contain both types of data.

3c3
  • 1
0

As long as there is exactly two types you can use and you know them at compile time, I would not look further than a simple if. It works, it's easy, everyone understands it.

You can do it totally dynamic at runtime, but that's a huge pain in the... where you don't want it to be. If you really want to go down that rabbit hole, you can find more information here.

Community
  • 1
  • 1
nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • I like your solution, in the Dev the if would do it, but eventually it would need to be completely dynamic based on the class. I have some reading to do thanks for the resources. – Caz1224 Apr 19 '16 at 05:53