Dynamic casting

G

Guest

Hi

I am using a DynamicProxy (class that inherits from RealProxy). I don't know the type of my Transparant Proxy at compile time. Is there a possibility to have a dynamic casting

object[] correctParams = new object[x]
for(int i...

Type t = Type.GetType(tiB.GetTypeName())
DynamicProxy proxy = new DynamicProxy(t, orderedParams, tiA)
correctParams = /*TODO: CAST into type t*/ proxy.GetTransparentProxy()


I would like to cast the result of proxy.GetTransparentProxy() into the type t. Is it possible

Thanks a lo
Valéry Bezençon
 
J

Jon Skeet [C# MVP]

=?Utf-8?B?VmFs?= said:
I am using a DynamicProxy (class that inherits from RealProxy).
I don't know the type of my Transparant Proxy at compile time.
Is there a possibility to have a dynamic casting?

object[] correctParams = new object[x];
for(int i...)
{
Type t = Type.GetType(tiB.GetTypeName());
DynamicProxy proxy = new DynamicProxy(t, orderedParams, tiA);
correctParams = /*TODO: CAST into type t*/ proxy.GetTransparentProxy();
}

I would like to cast the result of proxy.GetTransparentProxy()
into the type t. Is it possible?


What difference would it make? correctParams is just an array of object
references - why would you need to cast the reference?
 
G

Guest

----- Jon Skeet [C# MVP] wrote: ----

=?Utf-8?B?VmFs?= said:
I am using a DynamicProxy (class that inherits from RealProxy).
I don't know the type of my Transparant Proxy at compile time.
Is there a possibility to have a dynamic casting
object[] correctParams = new object[x]
for(int i...

Type t = Type.GetType(tiB.GetTypeName())
DynamicProxy proxy = new DynamicProxy(t, orderedParams, tiA)
correctParams = /*TODO: CAST into type t*/ proxy.GetTransparentProxy()
I would like to cast the result of proxy.GetTransparentProxy()
into the type t. Is it possible


What difference would it make? correctParams is just an array of object
references - why would you need to cast the reference

I need to use the methods contained in the Type t on the objects of correctParams in the rest of the code. If these object are just object, I will only be able to use the methods of the class object (GetType, GetHashCode, ...
 
J

Jon Skeet [C# MVP]

[Please fix your newsreader to wrap text at about 72 columns - your
current long lines make it a pain to reply to you.]

=?Utf-8?B?dmFs?= said:
What difference would it make? correctParams is just an array
of object references - why would you need to cast the reference?

I need to use the methods contained in the Type t on the objects of
correctParams in the rest of the code. If these object are just
object, I will only be able to use the methods of the class object
(GetType, GetHashCode, ...)

But casting where you suggested casting wouldn't do you any good
anyway. Actually, it would check that the cast it valid, but that's
all.

If you don't know the type in advance, you won't be able to use the
methods in your code anyway, will you? If you know that the type will
have certain methods, then build an interface with those methods, make
the type implement those methods, and cast to the interface when you
need to.
 
G

Guest

----- Jon Skeet [C# MVP] wrote: ----

[Please fix your newsreader to wrap text at about 72 columns - your
current long lines make it a pain to reply to you.

=?Utf-8?B?dmFs?= said:
What difference would it make? correctParams is just an array
of object references - why would you need to cast the reference
correctParams in the rest of the code. If these object are jus
object, I will only be able to use the methods of the class object
(GetType, GetHashCode, ...

But casting where you suggested casting wouldn't do you any good
anyway. Actually, it would check that the cast it valid, but that's
all

If you don't know the type in advance, you won't be able to use the
methods in your code anyway, will you? If you know that the type will
have certain methods, then build an interface with those methods, make
the type implement those methods, and cast to the interface when you
need to

Yes, you are right, I was a little bit confused. Thanks for your hel
Valéry
 

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

Similar Threads

dynamic generic creation using Type.GetType(string)? 2
Casting of generics? 3
Dynamic Casting 5
Dynamic data casting 3
Generics a few questions 3
C# casting issues. 2
casting from object 2
Dynamic cast possible? 4

Top