Reflecting a property type for IList

M

Michael Groeger

Hi,

is it possible to determine if the PropertyType of a property support IList?

We are inspecting all properties of a Type and we need to care about the
properties which have a type which supports IList. I tried to do the
following:

public void TestPropertiesOfType()
{
System.Type t = typeof (MyClass);
PropertyInfo[] properties = t.GetProperties();
foreach (PropertyInfo pi in properties)
{
if (pi.PropertyType is IList)
Console.WriteLine("Property {0} supports IList", pi.Name);
}
}

public class MyClass
{
public ArrayList MyArrayList
{
get { return _al; }
set { _al = value; }
}
}

ArrayList does support IList, but how can I find this one out? I know that
the above implmentation is incorrect because the Type returned by
pi.PropertyType itself does not support IList.

Any help is appreciated!

Regards,
Michael
 
C

Carlos J. Quintero [.NET MVP]

Try:

System.Type t = typeof (MyClass);
PropertyInfo[] properties = t.GetProperties();
Type type;

type = typeof(IList);

foreach (PropertyInfo pi in properties)
{
if (pi.PropertyType.GetInterface(type.Name) != null)
MessageBox.Show("Property {0} supports IList", pi.Name);
}

--

Carlos J. Quintero

MZ-Tools 4.0: Productivity add-ins for Visual Studio .NET
You can code, design and document much faster.
http://www.mztools.com
 
M

Michael Groeger

Carlos,

thanks for your reply. Great, it works !

Regards,
Michael

Carlos J. Quintero said:
Try:

System.Type t = typeof (MyClass);
PropertyInfo[] properties = t.GetProperties();
Type type;

type = typeof(IList);

foreach (PropertyInfo pi in properties)
{
if (pi.PropertyType.GetInterface(type.Name) != null)
MessageBox.Show("Property {0} supports IList", pi.Name);
}

--

Carlos J. Quintero

MZ-Tools 4.0: Productivity add-ins for Visual Studio .NET
You can code, design and document much faster.
http://www.mztools.com


Michael Groeger said:
Hi,

is it possible to determine if the PropertyType of a property support
IList?

We are inspecting all properties of a Type and we need to care about the
properties which have a type which supports IList. I tried to do the
following:

public void TestPropertiesOfType()
{
System.Type t = typeof (MyClass);
PropertyInfo[] properties = t.GetProperties();
foreach (PropertyInfo pi in properties)
{
if (pi.PropertyType is IList)
Console.WriteLine("Property {0} supports IList", pi.Name);
}
}

public class MyClass
{
public ArrayList MyArrayList
{
get { return _al; }
set { _al = value; }
}
}

ArrayList does support IList, but how can I find this one out? I know that
the above implmentation is incorrect because the Type returned by
pi.PropertyType itself does not support IList.

Any help is appreciated!

Regards,
Michael
 

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