Reflection question

B

Ben Fidge

I'm using Reflection to obtain data about a given objects public properties.
I need to perform different actions depening on whether a properties class
type is derived from one of the collection classes or not.

Take the following theoretical classes:

public class MyClass {
private int FiSimpleValue = 0;
private MyCollection FoCollectionValue = new MyCollection();

public int SimpleValue {
get {
return FiSimpleValue;
}
set {
FiSimpleValue = value;
}
}
public MyCollection CollectionValue {
get {
return FoCollectionValue;
}
}
}

public class MyCollection : ArrayList {
}


I need to be able to use reflection to read off the MyClass properties and
when I encounter the CollectionValue property, be able to determine at
runtime that it's type is derived from ArrayList. This is a simple example,
but in the real-world MyClass will have multiple properties of varying types
that are all derived from ArrayList. I need to take specific action for
these properties.

Thanks

Ben Fidge
 
M

Mattias Sjögren

Ben,

To check the direct base class of the property type

if ( yourPropInfo.PropertyType.BaseType == typeof(ArrayList) )

If you have deeper inheritance hierarchies, you may want to use this
instead

if ( typeof(ArrayList).IsAssignableFrom( yourPropInfo.PropertyType ) )



Mattias
 

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

Top