usable of typeof

  • Thread starter Thread starter laurent.sass
  • Start date Start date
L

laurent.sass

Hello,

I have made this code in a class

private static Type[] Etats = new Type[]
{
typeof(Transition1),
typeof(Transition2),
typeof(Transition3)
}

i with with an indice of a table, calling a static method.
for helping me, all my transactions are inherited about an Transaction
object.

I'll wish like to do this :

Transition myTransitionGenerique;
(myTransitionGenerique as Etats[Idx]).MaProprieteStatique();

Do you know if it is possible ?
I'm using this for making an State Machin without create all transition
object.

Thanks
 
Hello,

I have made this code in a class

private static Type[] Etats = new Type[]
{
typeof(Transition1),
typeof(Transition2),
typeof(Transition3)
}

i with with an indice of a table, calling a static method.
for helping me, all my transactions are inherited about an Transaction
object.

I'll wish like to do this :

Transition myTransitionGenerique;
(myTransitionGenerique as Etats[Idx]).MaProprieteStatique();

Do you know if it is possible ?
I'm using this for making an State Machin without create all transition
object.

Thanks

Hi Laurent,

The 'as' operator is a compile-time construct. It won't be of any help
here. Does 'MaProprieteStatique' get defined in your base 'Transition'
class? If so, you don't need to cast at all. You just need to instantiate
your object like this:

///
Transition mt = Activator.CreateInstance( Etats[Idx] ) as Transition;

if ( mt != null )
mt.MaProprieteStatique();
///

Hope this helps,
-- Tom Spink
 
Tom, static properties cannot be used via instance of an object (judging
from the name of the property member it is static property)
///
Transition mt = Activator.CreateInstance( Etats[Idx] ) as Transition;

if ( mt != null )
mt.MaProprieteStatique();
///

so your code won't do

Laurent,

Your code also won't work because typeof gives the Type object for the
actuall type and as such it doesn't have these static properties, but you
are on the right track.
What you need to do is to use a reflection and read the static proeprties
values using the Type objects
it is going to be something like

Transition myTransitionGenerique;
myTransitionGenerique =
Etats[Idx]).GetProperty("MaProprieteStatique").GetValue(null,null) as
Transition;

If MaProprieteStatique is declared on the level of a base class you should
probably use GetProperty overload that accept BindingFlags and pass
BindingFlags.FlattenHierarchy.


--
HTH
Stoitcho Goutsev (100)


Tom Spink said:
Hello,

I have made this code in a class

private static Type[] Etats = new Type[]
{
typeof(Transition1),
typeof(Transition2),
typeof(Transition3)
}

i with with an indice of a table, calling a static method.
for helping me, all my transactions are inherited about an Transaction
object.

I'll wish like to do this :

Transition myTransitionGenerique;
(myTransitionGenerique as Etats[Idx]).MaProprieteStatique();

Do you know if it is possible ?
I'm using this for making an State Machin without create all transition
object.

Thanks

Hi Laurent,

The 'as' operator is a compile-time construct. It won't be of any help
here. Does 'MaProprieteStatique' get defined in your base 'Transition'
class? If so, you don't need to cast at all. You just need to
instantiate
your object like this:

///
Transition mt = Activator.CreateInstance( Etats[Idx] ) as Transition;

if ( mt != null )
mt.MaProprieteStatique();
///

Hope this helps,
-- Tom Spink
 
Stoitcho said:
Tom, static properties cannot be used via instance of an object (judging
from the name of the property member it is static property)
///
Transition mt = Activator.CreateInstance( Etats[Idx] ) as Transition;

if ( mt != null )
mt.MaProprieteStatique();
///

so your code won't do

Laurent,

Your code also won't work because typeof gives the Type object for the
actuall type and as such it doesn't have these static properties, but you
are on the right track.
What you need to do is to use a reflection and read the static proeprties
values using the Type objects
it is going to be something like

Transition myTransitionGenerique;
myTransitionGenerique =
Etats[Idx]).GetProperty("MaProprieteStatique").GetValue(null,null) as
Transition;

If MaProprieteStatique is declared on the level of a base class you should
probably use GetProperty overload that accept BindingFlags and pass
BindingFlags.FlattenHierarchy.


Hi,

Tom, static properties cannot be used via instance of an object (judging
from the name of the property member it is static property)

By his declaration, I assumed it was a method. Properties do not have
parentheses on the end, as per his original code. Still, you are correct
that static methods/properties are inaccessible by instance.

-- Tom Spink
 
Tom Spink said:
(e-mail address removed) wrote:
The 'as' operator is a compile-time construct.

That's not true, is it? My understanding is that 'as' interrogates the
actual type of the object referred to by the variable, which is a runtime
operation.

///ark
 
Mark said:
That's not true, is it? My understanding is that 'as' interrogates the
actual type of the object referred to by the variable, which is a runtime
operation.

Left side evaluated at run-time, right-side evaluated at compile
time.

some what analogous to "If (x == 5)" where you can change the value
of "x" at run-time, but you can't change the value of "5".
 
Mark said:
That's not true, is it? My understanding is that 'as' interrogates the
actual type of the object referred to by the variable, which is a runtime
operation.

///ark

Hi Mark,

The 'as' operator does actually correspond to the MSIL instruction isinst,
which pops and tests the current object on the evaluation stack to see if
it's an instance of the supplied type, pushing null onto the stack if it
isn't and pushing the casted object to the evaluation stack.

You are totally correct, this is done at runtime, as it's dynamic type
checking, I guess I could have worded my response differently. I guess
what I meant to say is that casting is not dynamic. You cannot cast to a
type at runtime, where the type you want to cast to is dynamic.

Am I making any sense?

-- Tom Spink
 
Tom Spink said:
You are totally correct, this is done at runtime, as it's dynamic type
checking, I guess I could have worded my response differently. I guess
what I meant to say is that casting is not dynamic. You cannot cast to a
type at runtime, where the type you want to cast to is dynamic.

Am I making any sense?

Yes, thanks. Your and James's responses cleared up my confusion.

///ark
 
The whole snippet is a mess, but as far as my french goes 'Propriete
Statique' means 'Static Property'


--
Stoitcho Goutsev (100)

Tom Spink said:
Stoitcho said:
Tom, static properties cannot be used via instance of an object (judging
from the name of the property member it is static property)
///
Transition mt = Activator.CreateInstance( Etats[Idx] ) as Transition;

if ( mt != null )
mt.MaProprieteStatique();
///

so your code won't do

Laurent,

Your code also won't work because typeof gives the Type object for the
actuall type and as such it doesn't have these static properties, but you
are on the right track.
What you need to do is to use a reflection and read the static proeprties
values using the Type objects
it is going to be something like

Transition myTransitionGenerique;
myTransitionGenerique =
Etats[Idx]).GetProperty("MaProprieteStatique").GetValue(null,null) as
Transition;

If MaProprieteStatique is declared on the level of a base class you
should
probably use GetProperty overload that accept BindingFlags and pass
BindingFlags.FlattenHierarchy.


Hi,

Tom, static properties cannot be used via instance of an object (judging
from the name of the property member it is static property)

By his declaration, I assumed it was a method. Properties do not have
parentheses on the end, as per his original code. Still, you are correct
that static methods/properties are inaccessible by instance.

-- Tom Spink
 
<[email protected]> a écrit dans le message de (e-mail address removed)...

| I have made this code in a class
|
| private static Type[] Etats = new Type[]
| {
| typeof(Transition1),
| typeof(Transition2),
| typeof(Transition3)
| }
|
| i with with an indice of a table, calling a static method.
| for helping me, all my transactions are inherited about an Transaction
| object.
|
| I'll wish like to do this :
|
| Transition myTransitionGenerique;
| (myTransitionGenerique as Etats[Idx]).MaProprieteStatique();
|
| Do you know if it is possible ?

Malheureusement, non.

Vous essayez de transformer une instance d'un objet vers une instance d'un
type et ce n'est pas possible.

Vous étiez développeur Delphi ? Là, vous avez les types « classes » qui
peuvent posséder les membres virtuelle, mais avec .NET, ni les propriétés,
ni les méthodes et ni les constructeurs statiques participent dans le
polymorphisme.

Joanna
 

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

Back
Top