Check for Inheritance

  • Thread starter Thread starter Jason
  • Start date Start date
J

Jason

I want to use reflection to check if a given class inherits from
UserControl. The solution would need to consider the possibility that the
class does not inherit directly from UserControl but goes through several
layers. Besides creating an instance of the control and doing something like
"myControl Is UserControl", can I use reflection to determine this?

Thanks.

- Jason
 
Jason said:
I want to use reflection to check if a given class inherits from
UserControl. The solution would need to consider the possibility that the
class does not inherit directly from UserControl but goes through several
layers. Besides creating an instance of the control and doing something like
"myControl Is UserControl", can I use reflection to determine this?

Type.IsAssignableFrom is probably the easiest way.

if (typeof(UserControl).IsAssignableFrom (testType))
 
bool DoesItDeriveFromUserControl(string typename)
{
bool ret = false;
for( Type t = Type.GetType(typename); t.BaseClass; t != typeof(object); t = t.BaseType)
{
if( t == typeof(UserControl) )
{
ret = true;
break;
}
}
return ret;
}

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.framework/<[email protected]>

I want to use reflection to check if a given class inherits from
UserControl. The solution would need to consider the possibility that the
class does not inherit directly from UserControl but goes through several
layers. Besides creating an instance of the control and doing something like
"myControl Is UserControl", can I use reflection to determine this?

Thanks.

- Jason



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004



[microsoft.public.dotnet.framework]
 
I vote mine as the worst solution ;-)

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.framework/<#[email protected]>

bool DoesItDeriveFromUserControl(string typename)
{
bool ret = false;
for( Type t = Type.GetType(typename); t.BaseClass; t != typeof(object); t = t.BaseType)
{
if( t == typeof(UserControl) )
{
ret = true;
break;
}
}
return ret;
}

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.framework/<[email protected]>

I want to use reflection to check if a given class inherits from
UserControl. The solution would need to consider the possibility that the
class does not inherit directly from UserControl but goes through several
layers. Besides creating an instance of the control and doing something like
"myControl Is UserControl", can I use reflection to determine this?

Thanks.

- Jason



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004



[microsoft.public.dotnet.framework]

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004



[microsoft.public.dotnet.framework]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top