views:

39

answers:

2

I'm getting a Type using Assembly class as follows:

var asm = Assembly.GetAssembly(typeof(MyAssembly));
var t=asm.GetType("FULLY QUALIFIED CLASS NAME", true, true);

Then I create object from this type:

var obj = Activator.CreateObject(t, new []{ params });

Now I want to convert or cast this object to a Generic object (actually SubSonic Active Record Object).

var record = (ActiveRecord<PUT SOMEHOW TYPE t HERE>)obj;

How can I accomplish this?

A: 

your class must inherit ActiveRecord or by itself be ActiveRecord .

you are trying to cast a type into another object receving the fomer as varible:

Cat c = new Cat();
List<Cat> l = (List<Cat>) c; // error.
+3  A: 

The point of static typing is that you know the type at compile time.

What do you expect the type of the record variable to be? The compiler needs to know - it can't wait until execution time.

What do you want to do with record anyway? If the real goal is to create an ActiveRecord<T> object but you don't need to know the T for any other operations, then you'll need to use reflection with Type.MakeGenericType or MethodInfo.MakeGenericMethod depending on the ActiveRecord API (which I'm not familiar with)... but you're not going to be able to use the result in a statically typed way (that depends on T) in the lines of code which follow.

Does ActiveRecord<T> implement a nongeneric interface? If so, that's what you'd usually use after constructing the relevant instance.

If you can provide a link to ActiveRecord<T> documentation to show how to construct an instance, I'm happy to write the reflection code for you...

Jon Skeet