reflection, GetType()

H

hdjim

Hello, I'm trying to use the type returned by GetType as the declared
type of a variable but can't seem to get it working. Would appreciate
any help

Here is the code:

//gets loaded from db on startup
string Model = "ZZZ";
string sBase = "Base";
string state = "VA";

Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();

string baseType = Model + "." + sBase + "." + "Manager";
Type BaseObjType = assembly.GetType(baseType);

I now want to use BaseObjType as the declared type like so
(BaseObjType) myType;

But the above does not work. How to use the return type from GetType()
as the reference type of a variable?
TIA
hd
 
J

Jon Skeet [C# MVP]

Hello, I'm trying to use the type returned by GetType as the declared
type of a variable but can't seem to get it working. Would appreciate
any help

The type of the variable needs to be known at compile-time by the
compiler. It can't possibly know that if you're not working out the
type until execution time.

Jon
 
J

Jon Skeet [C# MVP]

Hello, I'm trying to use the type returned by GetType as the declared
type of a variable but can't seem to get it working. Would appreciate
any help

The type of the variable needs to be known at compile-time by the
compiler. It can't possibly know that if you're not working out the
type until execution time.

Jon
 
J

Jon Skeet [C# MVP]

The type of the variable needs to be known at compile-time by the
compiler. It can't possibly know that if you're not working out the
type until execution time.

No idea why this got double-posted... apologies...

Jon
 
N

Nicholas Paldino [.NET/C# MVP]

hd,

You can't use it this way, you have to know at ^compile-time^ what the
type is in order for a cast to work this way.

You have to continue to use reflection to call methods on the instance.

A way to get around this is to have the type implement an interface, or
derive from a common base class (which your implementation overloads the
methods/properties of) and then cast to that, and access the members that
you need.
 
H

hdjim

  A way to get around this is to have the type implement an interface,
or
derive from a common base class (which your implementation overloads the
methods/properties of) and then cast to that, and access the members that
you need.
but BaseObjType is the correct type I need. When I step thru the code,
it shows me the correct type. Now I just need to use it as that type.
 
N

Nicholas Paldino [.NET/C# MVP]

You can't, because the compiled code doesn't know what that type is.
You loaded it at run time, so there is no way that the pre-compiled code can
know what to do with it.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

hdjim said:
On Dec 9, 12:16 pm, "Nicholas Paldino [.NET/C# MVP]"
<[email protected]> wrote:
A way to get around this is to have the type implement an interface,
or
derive from a common base class (which your implementation overloads the
methods/properties of) and then cast to that, and access the members that
you need.
but BaseObjType is the correct type I need. When I step thru the code,
it shows me the correct type. Now I just need to use it as that type.
 
J

Jeff Johnson

but BaseObjType is the correct type I need. When I step thru the code,
it shows me the correct type. Now I just need to use it as that type.

Yes, but when you step through code the code is RUNNING. This has nothing to
do with COMPILE time, which is where your problem is.
 
H

hdjim

Yes, but when you step through code the code is RUNNING. This has nothingto
do with COMPILE time, which is where your problem is.

Damn! :(
So how does one create a declared type at runtime? I really need to
create a declared type at runtime.... :)
 
H

hdjim

If you want to do something else, then you should provide the precise  
definition of "declared type" that fits that scenario, so that we can  
better understand what it is you're actually trying to do.

Pete

Basically to distinguish a specific class I need to instantiate I use
distinct namespaces with a common class name. The namespace is
composed of model and state (any of the 50 states).

Examples:
x.va.Manager;
x.tx.Manager;
z.tx.Manager;
etc..

I also have a base namespace like x.base.Manager so if there is no
specific state implementation then this model.base implementation will
be create.

Now at startup I know the model and state and I am trying to avoid a
huge switch statement by creating the Model.base reference type (on
the left side of the variable) at runtime.

String Model = “x”; // comes from db at startup
String sBase = “Base”;

string baseType = Model + "." + sBase + "." + "Manager";

There is a Model + "." + sBase + "." + "Manager" in the project like
x.Base.Manager so I use assembly.GetType() like this:

Type BaseObjType = assembly.GetType(baseType);

Now at runtime BaseObjType shows exactly the type I need.

But I need to use it at compile time like so

BaseObjType myType;

But I can’t….:(
 
N

Nicholas Paldino [.NET/C# MVP]

As I've said in my previous post, you will have to have all of these
classes derive from a common base class (and override the virtual methods on
that) or implement an interface.

Then, using the Type instance, pass it to the static CreateInstance
method of the Activator class and cast the return type to the shared
interface/base class and call the methods on that.

This is a case where polymorphism is going to be your friend.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

hdjim said:
If you want to do something else, then you should provide the precise
definition of "declared type" that fits that scenario, so that we can
better understand what it is you're actually trying to do.

Pete

Basically to distinguish a specific class I need to instantiate I use
distinct namespaces with a common class name. The namespace is
composed of model and state (any of the 50 states).

Examples:
x.va.Manager;
x.tx.Manager;
z.tx.Manager;
etc..

I also have a base namespace like x.base.Manager so if there is no
specific state implementation then this model.base implementation will
be create.

Now at startup I know the model and state and I am trying to avoid a
huge switch statement by creating the Model.base reference type (on
the left side of the variable) at runtime.

String Model = “x”; // comes from db at startup
String sBase = “Base”;

string baseType = Model + "." + sBase + "." + "Manager";

There is a Model + "." + sBase + "." + "Manager" in the project like
x.Base.Manager so I use assembly.GetType() like this:

Type BaseObjType = assembly.GetType(baseType);

Now at runtime BaseObjType shows exactly the type I need.

But I need to use it at compile time like so

BaseObjType myType;

But I can’t….:(
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top