System.ValueType

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

My code is this:

void WriteOut(object ob)
{
Type pTy = ob.GetType();
if(pTy.BaseType is System.ValueType)
.....

I get this error:
The given expression is never of the provided ('System.ValueType') type

Maybe I am thinking about this wrong. I want to pass generic "objects" into this function and use reflection to display all the properties and their values.

Now my function works fine for "objects" like my custom Person or Customer object. But if I pass an Integer into the function I want to realize that it is of System.ValueType and just print the value....

It works fine if I say: If(pTy.BaseType.ToString() == "System.ValueType")

Why is this? If the BaseType.ToString() = "System.ValueType" then shouldn't the BaseType be of System.ValueType?

I hope all that made sence. Any insight would be appreciated.
 
<=?Utf-8?B?QnJpYW4gTGluZGVu?= <Brian
My code is this:

void WriteOut(object ob)
{
Type pTy = ob.GetType();
if(pTy.BaseType is System.ValueType)
.....

I get this error:
The given expression is never of the provided ('System.ValueType') type

And it's correct.
Maybe I am thinking about this wrong. I want to pass generic
"objects" into this function and use reflection to display all the
properties and their values.

Now my function works fine for "objects" like my custom Person or
Customer object. But if I pass an Integer into the function I want to
realize that it is of System.ValueType and just print the value....

It works fine if I say: If(pTy.BaseType.ToString() ==
"System.ValueType")

Why is this? If the BaseType.ToString() = "System.ValueType" then
shouldn't the BaseType be of System.ValueType?

I hope all that made sence. Any insight would be appreciated.

I think you're confused about what the "is" operator does. It takes a
reference expression on the left hand side and tests whether that is a
reference to an object which is an instance of the type specified on
the right hand side. It *doesn't* take a type on the left hand side and
compare it with the one on the right hand side. To make this concrete:

if (new MemoryStream() is Stream) // Yes
if (typeof(MemoryStream) is Stream) // No

In the latter case, the expression typeof(MemoryStream) is of type
Type, which clearly isn't a stream.

You can use Type.IsAssignableFrom to get the effect you want in
general. In your particular case, you could actually use

if (pTy.BaseType==typeof(ValueType))

because any value type *must* have ValueType as the base type

Alternatively, and rather better, you can use:

if (pTy.IsValueType)
 
<=?Utf-8?B?QnJpYW4gTGluZGVu?= <Brian
I am new to C#. I have been programming in VB.NET since the start of
it, but when I had to choose one of the express versions to play
around with, I thought I would give C# a try.

Now in VB.NET don't you use the "Is" operator to compare types? so
apparently it is not the same in C#. Or am I bass ackwards on my
logic.

In VB.NET you use the "Is" operator to compare two references. The
equivalent in C# is ==, if the types involved haven't overloaded the ==
operator. It's nothing to do with the C# "is" operator.
 
Back
Top