PropertyInfo.PropertyType is never ValueType?

  • Thread starter Thread starter INeedADip
  • Start date Start date
I

INeedADip

PropertyInfo[] props = obj.GetType().GetProperties();
foreach (PropertyInfo p in props)
if (p.PropertyType is ValueType)
this._commonProperties.Add(p.Name, p.GetValue(request,
null).ToString());

I get a warning that says p.PropertyType will never be ValueType....and it
was right.
After stepping through it the types are "System.Boolean" etc...
I want to add all integer, string, and boolean types to NameValueCollection,
but not objects.

I thought System.Boolean was a ValueType?
How should I test for this?
 
INeedADip,

PropertyInfo.PropertyType returns "System.Type", so you are actually
querying whether or not "System.Type" is a value-type, and since the compiler
knows it's not and will never be, you get a warning.

You should rewrite as follows:
if (p.PropertyType.IsValueType)
{
...
}

HTH,
Baileys
 

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