473,748 Members | 3,585 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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)ob j" 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 7593
Would this work for you (Note: typed from memory.) ?

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

--
William Stacey, MVP

"Laura" <la******@mm-games.com> wrote in message
news:bc******** *************** ***@posting.goo gle.com...
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)ob j" 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.goo gle.com...
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)ob j" 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
3548
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 sort of dynamically allocated array of : (1) void pointers, that must be cast into the desired types. (2) union of the desired pointers. (3) An Inheritance hierarchy (4) Possibly a union of the desired data types.
2
2539
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 will open a dialog to give more detailed information) . The number of controls on the main GUI will vary from client site to client site. I would therefore like to populate the dialog with these controls dynamically.
1
6285
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 variable (targetId) in to the usercontrol (IntergySite.aspx) by calling its setter method. Currently, I am using if-then-else and hardcoded the User Control Object to do casting and call the setter method. Question: Is there any way I could load,...
7
3195
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 -- UC/ ----- Classic/
11
3783
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
11081
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 how those properties are set. I want to be able to do two other things: a) add User control instances to my page, filling in the place of placeholder controls, and b) programmatically setting custom properties on those dynamically spawned...
2
3118
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 dynamically loaded assembly) so far so good (per my code below)... but here is where I'm getting hung up: 3. Call methods of that type (see comments in my code) If the types in the dynamically loaded assembly were in the same namespace
2
5113
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 = Windows Forms class B = a singleton hosted within A. B is responsible for dynamically loading classes X, Y, and Z.
4
11009
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 to iterate thru the gridview's rows, examine the databound controls, then perform a database action. for some reason, i can't find the controls. i have a two templates: one that uses a label the other uses a textbox to display data. when the...
0
8984
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9363
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9312
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8237
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.