473,387 Members | 1,572 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,387 software developers and data experts.

How to use "is" operator to a type from Reflection

I have trouble to use "Is" operator to a type that is from reflection.
Basically, here is what I want to do, if Class1 type is not from reflection:
class Class1
{
}
public class Test
{
...
if (o is Class1) //to determine if object o is Class1, or is derivative
class of Class1
{
//object o can be cast to type
}
}

Now, Class1 is in different assembly, then I want to the samething, except
getting the Type from reflection

public class Test
{
...
Assembly objAssembly = Assembly.Load("MyDll");
Type t = objAssembly.GetType("MyDll.Class1");

//How to determine if object o is Class1, or is derivative class of Class1
????????
//if (o is Class1)
}
Thank a lot for your kind response
Mar 24 '06 #1
4 1637
You should use the IsAssignableFrom() method of the Type class, like
this:

Assembly objAssembly = Assembly.Load("MyDll");
Type t = objAssembly.GetType("MyDll.Class1");
if (t.IsAssignableFrom(o))
{
... etc ...
}

I think that's the way it goes... I always get that IsAssignableFrom
thing backward. :) Anyway, I believe that's the method you want to
call. You can't use the "is" operator directly.

Mar 24 '06 #2
>Now, Class1 is in different assembly, then I want to the samething, except
getting the Type from reflection


I think you're looking for typeof(Class1).IsAssignableFrom(t);
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Mar 24 '06 #3
Yes, that is exactly I am looking for. Thanks mattias!

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:uz**************@TK2MSFTNGP11.phx.gbl...
Now, Class1 is in different assembly, then I want to the samething,
except
getting the Type from reflection


I think you're looking for typeof(Class1).IsAssignableFrom(t);
Mattias

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

Mar 24 '06 #4
You may want to read Jon Skeet's article on plug-ins and cast
exceptions.

http://www.yoda.arachsys.com/csharp/plugin.html

Mar 24 '06 #5

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

Similar topics

15
by: Bobby C. Jones | last post by:
Is there an advantage to either of these methods, or is it simply a matter of style? I used to use the "as" method, but later gave it up for the "is" method as the intent of the code seems a...
6
by: Derrick | last post by:
Hello all; Since I do have working code, this is more for my curiosity only. I'm creating a "Plugin" architecture, following some of the many examples on the 'net. Basically what I have is...
3
by: JimM | last post by:
I am trying to create a method in VS 2003 that validates an object argument is of the proper type and within a range of values. I am trying to use a Type to define the casting and object type for...
17
by: nicolas.hilaire | last post by:
Hi all, i've read this article http://msdn2.microsoft.com/en-us/library/85af44e9.aspx who first interest me much. I've translated it to use generic instead of template : generic < typename ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.