473,399 Members | 3,038 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

How to do dynamically cast in C#

In C#, the method was dynamically invoked through reflection. But How
do we dynamically cast the return object to its own type? Here is the
sample code:

// dynamically invoke the method
Object obj = mi.Invoke(null, null);

// get the type of the returned object
Type MyType = obj.GetType();

// Question: Is there a way "(MyType)obj" to cast the obj dynamically
in runtime?

One option is through interface implementation, another option is to
let the obj implement IConvertible interface.

I am looking for any other simple solution. Does c# have any special
syntax to achieve this?

Your idea will be appriciated.
Nov 15 '05 #1
2 7573
Would this work for you (Note: typed from memory.) ?

if ( obj.GetType() == typeof(MyClass) )
{
MyClass myClass = (MyClass)object;
myClass.DoSomething();
}
else
{
//...
}

--
William Stacey, MVP

"Laura" <la******@mm-games.com> wrote in message
news:bc**************************@posting.google.c om...
In C#, the method was dynamically invoked through reflection. But How
do we dynamically cast the return object to its own type? Here is the
sample code:

// dynamically invoke the method
Object obj = mi.Invoke(null, null);

// get the type of the returned object
Type MyType = obj.GetType();

// Question: Is there a way "(MyType)obj" to cast the obj dynamically
in runtime?

One option is through interface implementation, another option is to
let the obj implement IConvertible interface.

I am looking for any other simple solution. Does c# have any special
syntax to achieve this?

Your idea will be appriciated.

Nov 15 '05 #2
This probably means you are expecting the returned object to implement some
specific members. If so, create an interaface, and make sure that all
returned objects implement that interface. Then, you know that you can cast
to the interface and call whatever memeber you need to call.

-Rob Teixeira [MVP]

"Laura" <la******@mm-games.com> wrote in message
news:bc**************************@posting.google.c om...
In C#, the method was dynamically invoked through reflection. But How
do we dynamically cast the return object to its own type? Here is the
sample code:

// dynamically invoke the method
Object obj = mi.Invoke(null, null);

// get the type of the returned object
Type MyType = obj.GetType();

// Question: Is there a way "(MyType)obj" to cast the obj dynamically
in runtime?

One option is through interface implementation, another option is to
let the obj implement IConvertible interface.

I am looking for any other simple solution. Does c# have any special
syntax to achieve this?

Your idea will be appriciated.

Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Peter Olcott | last post by:
I want to be able to efficiently build data structures at run-time. These data structures need to be accessed with minimal time. The only a few ways that come immediately to mind would be some...
2
by: macca | last post by:
Hi, I am writing a GUI application. It will have a number of user defined controls( I plan to use/create a user defined control that will output alarm states that the user can also select and...
1
by: Reza Nabi | last post by:
Bakground: I have a webform (LoadCtl.aspx) which loads the user control to a placeholder dynamically based on the ctlName querystring passed in the URL. Webform (LoadCtl.aspx) also passes a...
7
by: Samuel | last post by:
Hi, I am building a page that makes use of user control as a templating technique. The following is that I have in mind and it is actually working: Root/ -- login.aspx -- login.aspx.vb --...
11
by: skumar434 | last post by:
Hi everybody, I am faceing problem while assigning the memory dynamically to a array of structures . Suppose I have a structure typedef struct hom_id{ int32_t nod_de; int32_t hom_id;
6
by: | last post by:
I have made some user controls with custom properties. I can set those properties on instances of my user controls, and I have programmed my user control to do useful visual things in response to...
2
by: Smithers | last post by:
Using 3.5, I am stuck in attempting to: 1. Dynamically load an assembly 2. Instantiate a class from that assembly (the client code is in a different namespace than the namespace of the...
2
by: Smithers | last post by:
I have a Windows Forms application that implements a plug-in architecture whereby required assemblies are identified and loaded dynamically. Here are the relevant classes: A = application =...
4
by: Craig Buchanan | last post by:
I dynamically add data-bound templates to a gridview in my ascx control. while this works correctly when the gridview is databound to the datatable, i'm having issues on postback. i would like...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.