When I have an instance of an object wich is of type System.Type, how
can I find out if it directly or indirecly derives from another type?
myTypeInstance == typeof(AnotherType)
only seems to check if they are ultimately the same type (at the very
bottom of the inheritance hirarchy) but in case myTypeInstance is
"TypeDerivedFromAnotherType" the above evaluates to false..
myTypeInstance.BaseType == typeof(AnotherType)
does the same, only for the direct base class. 8 2359
Hi,
Di dyou try Type.IsSubClassOf ()
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"bonk" <sc******************@gmx.dewrote in message
news:11*********************@m7g2000cwm.googlegrou ps.com...
When I have an instance of an object wich is of type System.Type, how
can I find out if it directly or indirecly derives from another type?
myTypeInstance == typeof(AnotherType)
only seems to check if they are ultimately the same type (at the very
bottom of the inheritance hirarchy) but in case myTypeInstance is
"TypeDerivedFromAnotherType" the above evaluates to false..
myTypeInstance.BaseType == typeof(AnotherType)
does the same, only for the direct base class.
Try:
myTypeInstance is AnotherType
--
David Anton www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
C# Code Metrics: Quick metrics for C#
"bonk" wrote:
When I have an instance of an object wich is of type System.Type, how
can I find out if it directly or indirecly derives from another type?
myTypeInstance == typeof(AnotherType)
only seems to check if they are ultimately the same type (at the very
bottom of the inheritance hirarchy) but in case myTypeInstance is
"TypeDerivedFromAnotherType" the above evaluates to false..
myTypeInstance.BaseType == typeof(AnotherType)
does the same, only for the direct base class.
The easiest way (although not the most correct way to do this) would be
to call IsAssignableFrom, like so:
typeof(AnotherType).IsAssignableFrom(myTypeInstanc e);
If AnotherType derives from myTypeInstance, then this will return true.
However, IsAssignableFrom does a lot more than just check the inheritance
chain, it checks interfaces, generic types, etc, etc.
A better solution is this:
static bool TypeDerivesFrom(Type base, Type derived)
{
// Removed checking against null for clarity.
// An easy assumption, not necessarily needed.
if (base == typeof(object))
{
// Return true.
return true;
}
// Compare while true.
do while (derived != null)
{
// If the base class is equal to the derived class, then
// return true.
if (base == derived)
{
// Get out.
return true;
}
// Set the derived class equal it's base type.
derived = derived.BaseType;
}
// The type does not derive from another type.
return false;
}
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"bonk" <sc******************@gmx.dewrote in message
news:11*********************@m7g2000cwm.googlegrou ps.com...
When I have an instance of an object wich is of type System.Type, how
can I find out if it directly or indirecly derives from another type?
myTypeInstance == typeof(AnotherType)
only seems to check if they are ultimately the same type (at the very
bottom of the inheritance hirarchy) but in case myTypeInstance is
"TypeDerivedFromAnotherType" the above evaluates to false..
myTypeInstance.BaseType == typeof(AnotherType)
does the same, only for the direct base class.
Wow - are you sure you're not just rewriting the "is" operator?
--
David Anton www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
C# Code Metrics: Quick metrics for C#
"Nicholas Paldino [.NET/C# MVP]" wrote:
The easiest way (although not the most correct way to do this) would be
to call IsAssignableFrom, like so:
typeof(AnotherType).IsAssignableFrom(myTypeInstanc e);
If AnotherType derives from myTypeInstance, then this will return true.
However, IsAssignableFrom does a lot more than just check the inheritance
chain, it checks interfaces, generic types, etc, etc.
A better solution is this:
static bool TypeDerivesFrom(Type base, Type derived)
{
// Removed checking against null for clarity.
// An easy assumption, not necessarily needed.
if (base == typeof(object))
{
// Return true.
return true;
}
// Compare while true.
do while (derived != null)
{
// If the base class is equal to the derived class, then
// return true.
if (base == derived)
{
// Get out.
return true;
}
// Set the derived class equal it's base type.
derived = derived.BaseType;
}
// The type does not derive from another type.
return false;
}
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"bonk" <sc******************@gmx.dewrote in message
news:11*********************@m7g2000cwm.googlegrou ps.com...
When I have an instance of an object wich is of type System.Type, how
can I find out if it directly or indirecly derives from another type?
myTypeInstance == typeof(AnotherType)
only seems to check if they are ultimately the same type (at the very
bottom of the inheritance hirarchy) but in case myTypeInstance is
"TypeDerivedFromAnotherType" the above evaluates to false..
myTypeInstance.BaseType == typeof(AnotherType)
does the same, only for the direct base class.
David,
Yeah, I realized that was exactly what I was doing.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"David Anton" <Da********@discussions.microsoft.comwrote in message
news:87**********************************@microsof t.com...
Wow - are you sure you're not just rewriting the "is" operator?
--
David Anton www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
C# Code Metrics: Quick metrics for C#
"Nicholas Paldino [.NET/C# MVP]" wrote:
> The easiest way (although not the most correct way to do this) would be to call IsAssignableFrom, like so:
typeof(AnotherType).IsAssignableFrom(myTypeInstan ce);
If AnotherType derives from myTypeInstance, then this will return true. However, IsAssignableFrom does a lot more than just check the inheritance chain, it checks interfaces, generic types, etc, etc.
A better solution is this:
static bool TypeDerivesFrom(Type base, Type derived) { // Removed checking against null for clarity.
// An easy assumption, not necessarily needed. if (base == typeof(object)) { // Return true. return true; }
// Compare while true. do while (derived != null) { // If the base class is equal to the derived class, then // return true. if (base == derived) { // Get out. return true; }
// Set the derived class equal it's base type. derived = derived.BaseType; }
// The type does not derive from another type. return false; }
Hope this helps.
-- - Nicholas Paldino [.NET/C# MVP] - mv*@spam.guard.caspershouse.com
"bonk" <sc******************@gmx.dewrote in message news:11*********************@m7g2000cwm.googlegro ups.com...
When I have an instance of an object wich is of type System.Type, how
can I find out if it directly or indirecly derives from another type?
myTypeInstance == typeof(AnotherType)
only seems to check if they are ultimately the same type (at the very
bottom of the inheritance hirarchy) but in case myTypeInstance is
"TypeDerivedFromAnotherType" the above evaluates to false..
myTypeInstance.BaseType == typeof(AnotherType)
does the same, only for the direct base class.
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:%2****************@TK2MSFTNGP03.phx.gbl...
David,
Yeah, I realized that was exactly what I was doing.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"David Anton" <Da********@discussions.microsoft.comwrote in message
news:87**********************************@microsof t.com...
>Wow - are you sure you're not just rewriting the "is" operator? -- David Anton www.tangiblesoftwaresolutions.com Instant C#: VB to C# converter Instant VB: C# to VB converter Instant C++: C#/VB to C++ converter C# Code Metrics: Quick metrics for C#
"Nicholas Paldino [.NET/C# MVP]" wrote:
>> The easiest way (although not the most correct way to do this) would be to call IsAssignableFrom, like so:
typeof(AnotherType).IsAssignableFrom(myTypeInsta nce);
If AnotherType derives from myTypeInstance, then this will return true. However, IsAssignableFrom does a lot more than just check the inheritance chain, it checks interfaces, generic types, etc, etc.
A better solution is this:
static bool TypeDerivesFrom(Type base, Type derived) { // Removed checking against null for clarity.
// An easy assumption, not necessarily needed. if (base == typeof(object)) { // Return true. return true; }
// Compare while true. do while (derived != null) { // If the base class is equal to the derived class, then // return true. if (base == derived) { // Get out. return true; }
// Set the derived class equal it's base type. derived = derived.BaseType; }
// The type does not derive from another type. return false; }
Hope this helps.
-- - Nicholas Paldino [.NET/C# MVP] - mv*@spam.guard.caspershouse.com
"bonk" <sc******************@gmx.dewrote in message news:11*********************@m7g2000cwm.googlegr oups.com... When I have an instance of an object wich is of type System.Type, how can I find out if it directly or indirecly derives from another type?
myTypeInstance == typeof(AnotherType)
only seems to check if they are ultimately the same type (at the very bottom of the inheritance hirarchy) but in case myTypeInstance is "TypeDerivedFromAnotherType" the above evaluates to false..
myTypeInstance.BaseType == typeof(AnotherType)
does the same, only for the direct base class.
lol exactly what I was doing on another thread :P I was using reflection
instead of the "is" operator heh.
must be a virus floating around affecting us...
Mythran
I'm a lazy programmer - sometimes that pays off, sometimes it doesn't ;)
--
David Anton www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
C# Code Metrics: Quick metrics for C#
"Nicholas Paldino [.NET/C# MVP]" wrote:
David,
Yeah, I realized that was exactly what I was doing.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"David Anton" <Da********@discussions.microsoft.comwrote in message
news:87**********************************@microsof t.com...
Wow - are you sure you're not just rewriting the "is" operator?
--
David Anton www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
C# Code Metrics: Quick metrics for C#
"Nicholas Paldino [.NET/C# MVP]" wrote:
The easiest way (although not the most correct way to do this) would
be
to call IsAssignableFrom, like so:
typeof(AnotherType).IsAssignableFrom(myTypeInstanc e);
If AnotherType derives from myTypeInstance, then this will return
true.
However, IsAssignableFrom does a lot more than just check the inheritance
chain, it checks interfaces, generic types, etc, etc.
A better solution is this:
static bool TypeDerivesFrom(Type base, Type derived)
{
// Removed checking against null for clarity.
// An easy assumption, not necessarily needed.
if (base == typeof(object))
{
// Return true.
return true;
}
// Compare while true.
do while (derived != null)
{
// If the base class is equal to the derived class, then
// return true.
if (base == derived)
{
// Get out.
return true;
}
// Set the derived class equal it's base type.
derived = derived.BaseType;
}
// The type does not derive from another type.
return false;
}
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"bonk" <sc******************@gmx.dewrote in message
news:11*********************@m7g2000cwm.googlegrou ps.com...
When I have an instance of an object wich is of type System.Type, how
can I find out if it directly or indirecly derives from another type?
myTypeInstance == typeof(AnotherType)
only seems to check if they are ultimately the same type (at the very
bottom of the inheritance hirarchy) but in case myTypeInstance is
"TypeDerivedFromAnotherType" the above evaluates to false..
myTypeInstance.BaseType == typeof(AnotherType)
does the same, only for the direct base class.
David Anton schrieb:
Try:
myTypeInstance is AnotherType
--
This will always evaluate to false since myTypeInstance is always of
type System.Type (see my original post) and AnotherType is not. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Amir |
last post by:
Hi all,
I have a table called PTRANS with few columns (see create script below).
I have created a view on top that this table VwTransaction (See below)
I can now run this query without a...
|
by: andrew |
last post by:
C:\Documents and Settings\watts\My Documents\Visual Studio
Projects\boeing\showPMACfg\vuePMAcfg\vuePMAcfg.cs(88):
Static member 'vuePMAcfg.pmaDataHash.pmaDataHash1' cannot
be accessed with an...
|
by: Daniel |
last post by:
Hello,
This is maybe a simple one:
Is there any way to check, if a string can be converted to
another type (like DateTime or Integer) without using
try/catch? In VB, i can use IsDate or...
|
by: Daniel |
last post by:
Hello,
maybe this is an easy one:
Is there any way to check, if a string value can be
converted into another type (e.g. DateTime, Integer)
without using exceptions (try/catch)?
Thanks in...
|
by: m96 |
last post by:
hi,
i'm trying to make a query to a ldap server (version v2 or v3 doen't
matter) with c#. the query works just fine but the problem is that i can't
read the custom attributes/fields, since .net...
|
by: Martin Jørgensen |
last post by:
Hi,
Short question:
Any particular reason for why I'm getting a warning here:
(cast from function call of type int to non-matching type double)
xdouble = (double)rand()/(double)RAND_MAX;
|
by: tadmill |
last post by:
Is it possible for a class that accepts a generic type, to re-create
another instance of itself with a property type of the passed class?
ex.
public class SomeClass<T>
{
private PropertyInfo...
|
by: DamienS |
last post by:
Hi,
I need to determine if one type is inheritted from another type. I
don't have instances of the objects (and it's undesirable to create
them).
Can someone please point me in the right...
|
by: alireza6485 |
last post by:
Hi:
I have 3 class/Methods:
the first one:just beeps
the second one:ask the user to Enter the number of beep
The last one:Activate and runs.
I get the following Error:"instance reference;...
|
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,...
|
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...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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...
|
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: 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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |