views:

361

answers:

2

I am trying to create an instance of a typed dataset dynamically in my code at runtime. I have the type available to me, but when I try to do this:

object obj = Activator.CreateInstance(Type.GetType("TYPED DATASET TYPE HERE"));

The problem is the type doesn't seem to be valid according to the code when I try and run it. What could I be doing wrong here?

+3  A: 

Where is the type defined? (which assembly).

Unless you give it an assembly qualified name, it will only look in the calling assembly, and a few other key assemblies. Options:

  • use an assembly qualified name ("somen.amespace.sometype, someassembly, ...")
  • get the Assembly instance (from a known type in that assembly), and use GetType(fullyQualifiedName) on the Assembly instance
Marc Gravell
Thanks for the suggestion! What I ended up doing was creating a local known instance, then calling gettype() on that object, and using the properties from it. Specifying the assembly seemed to solve that issue.
Noah
A: 

Assuming you are emitting the correct dataset code you may also need to load the assembly.

Look into the following .net namespaces reflection.emit, and reflection.

You could also look at the open source projects such as windsor which use reflection to emit new classes to create interceptors. There may be code in there that you can learn from.

Peter