Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / .NET SDK / October 2004

Tip: Looking for answers? Try searching our database.

how to do dynamic cast in C#

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
cindy liu - 01 Oct 2004 20:21 GMT
Hi Everyone,

I want to create an instance of a dynamic class in run time. The type of the
dynamic class is unknown during the compile time. I can use
Activator.CreateInstance to instantiate the dynamic class. But it returns
object data type. How can I get the instansce of the dynamic class as its own
data type?

Thanks in advance!!!
Cindy
Mattias Sj?gren - 01 Oct 2004 22:07 GMT
Cindy,

>How can I get the instansce of the dynamic class as its own data type?

Since the type is unknown at compile time, you can't declare a
variable of that type. So there's no point in such a cast.

Mattias

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

tom wisnowski - 05 Oct 2004 06:19 GMT
Can you be a little clearer on what you mean by dynamic class?

If you mean that you are dynamically "loading" a class, using its type name,
you can do a cast from object to type by either:
   typedvar = (type)objvar
   typedvar = objvar as type
But since you are dynimally loading the type, you must be in a situation
where the type is not available to you during compilation. In this case, you
will either have to use runtime reflection to access your members or bind to
a common interface

If you are dynamically generating the type, and then dynamically loading it,
you must use reflection or a predefined interface to access the type's members

Hope this helps

Tom Wisnowski
Statera
MCP MCAD

> Hi Everyone,
>
[quoted text clipped - 6 lines]
> Thanks in advance!!!
> Cindy
cindy liu - 05 Oct 2004 20:07 GMT
Thanks everyone! I found out I don't need to cast it at all.
Cindy

> Hi Everyone,
>
[quoted text clipped - 6 lines]
> Thanks in advance!!!
> Cindy

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.