Hello,
Is there a way to cast to another type when all we have is the System.Type.
Say, if we have:
Class MyBaseClass;
Class MyDerivedClass: MyBaseClass;
instead of:
MyDerivedClass derivedObject = new MyDerivedClass( );
MyBaseClass baseObject = (MyBaseClass) derivedObject;
something like this:
public object CastToBaseClass ( object someObject )
{
System.Type baseType = someObject.GetT ype().BaseType;
//now what? :S
object baseObject = (/* what goes here?*/) someObject;
//in my dreams: ... keyword "classof", inverse of "typeof"
// object baseObject = (classof( baseType ) ) someObject; :) :) :)
}
Anything like that in the framework?
Thanks 3 1389
Abelardo,
No there is not. How would you know what to call at compile time? You
will have to use reflection to do what you want.
Either that, or expose an interface that the type implements, and cast
the instance to that interface.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"Abelardo Vacca" <Ab***********@ discussions.mic rosoft.com> wrote in message
news:0F******** *************** ***********@mic rosoft.com... Hello,
Is there a way to cast to another type when all we have is the System.Type.
Say, if we have: Class MyBaseClass; Class MyDerivedClass: MyBaseClass;
instead of: MyDerivedClass derivedObject = new MyDerivedClass( ); MyBaseClass baseObject = (MyBaseClass) derivedObject;
something like this: public object CastToBaseClass ( object someObject ) { System.Type baseType = someObject.GetT ype().BaseType;
//now what? :S
object baseObject = (/* what goes here?*/) someObject; //in my dreams: ... keyword "classof", inverse of "typeof" // object baseObject = (classof( baseType ) ) someObject; :) :) :) }
Anything like that in the framework?
Thanks
but if there is two versions of one method created using "new" keyword and
you want to call base class's implementation?
If you want to call the base class implementation, then get the type of
the base class and then reflect to get the MethodInfo of the shadowed
method.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"The Crow" <q> wrote in message
news:OR******** ******@TK2MSFTN GP10.phx.gbl... but if there is two versions of one method created using "new" keyword and you want to call base class's implementation? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Jacob Jensen |
last post by:
This question has probably been asked a million time, but here it comes
again. I want to learn the difference between the three type cast operators:
static_cast, reinterpret_cast, dynamic_cast. A good way to do this is by
example. So I will give an example and please tell me what you think:
I have a base
class A
with a virtual destructor, and a
class B
that is it inherits publicly from A and defines som extra stuff.
|
by: Ramesh Tharma |
last post by:
Hi,
Is any one knows what's wrong with the following code, I was told that it
will compile and run but it will crash for some values.
Assume that variables are initilized.
char* c;
long* lg;
|
by: Jean Stax |
last post by:
Hi !
Jeffrey Richter gives the following example in his book:
struct Point {
Int32 x, y;
}
class AAA{
....
public virtual void Add(Object value);
|
by: Ken Allen |
last post by:
I am relatively new to .Net, but have been using VB and C/C++ for years.
One of the drawbacks with VB6 and earlier was the difficulty in casting a
'record' to a different 'shape' so one could perform different manipulations
on it. For example, I have a complex data structure, which I can represent
in a VB6 TYPE declaration, but I cannot easily convert that to a fixed
length array of unsigned bytes so that I could perform a checksum...
|
by: Frederic Rentsch |
last post by:
Hi all,
If I derive a class from another one because I need a few extra
features, is there a way to promote the base class to the derived one
without having to make copies of all attributes?
class Derived (Base):
def __init__ (self, base_object):
# ( copy all attributes )
...
| |
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...
|
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...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
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...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |