Need to check if the propery of an object exits.

J

JoeP

Hi All,

Need to check if the propery of an object exits.

oMyObject.Message

In some cases the Message property may not exists. I know the GetType() can
do it.
How would you check it?

Thanks,

Joe
 
R

Robbe Morris - [MVP] C#

System.Reflection and iterate through the PropertyInfo[].

The Type of the object has a .GetProperties method.
 
J

JoeP

Hi Robbin,

Thanks for your reply.

Here is the situation I have 2 objects such as: Execption or SMTPExecption
The SMTPExecption has the property StatusCode while the other does not.

I pass the the object into an error handle method where I log the properties into a text file.
Looking at the Watch window it seems that the Execption does not have System.Reflection etc...

Regards,

Joe


Robbe Morris - [MVP] C# said:
System.Reflection and iterate through the PropertyInfo[].

The Type of the object has a .GetProperties method.

--
Robbe Morris [Microsoft MVP - Visual C#]
.NET Setup Deployment - MSI, Cassini, SQL Server, NTFS
http://www.eggheadcafe.com/tutorial...5ab-35ca33da0f65/net-setup-deployment--m.aspx



JoeP said:
Hi All,

Need to check if the propery of an object exits.

oMyObject.Message

In some cases the Message property may not exists. I know the GetType()
can do it.
How would you check it?

Thanks,

Joe
 
R

Robbe Morris - [MVP] C#

And when you performed an

Type myType = myException.GetType();

PropertyInfo[] properties = myType.GetProperties();

what did you get?

--
Robbe Morris [Microsoft MVP - Visual C#]
AdvancedXL Server, Designer, and Data Analyzer
Convert cell ranges in Excel to rule driven web surveys
http://www.equalssolved.com/default.aspx




Hi Robbin,

Thanks for your reply.

Here is the situation I have 2 objects such as: Execption or SMTPExecption
The SMTPExecption has the property StatusCode while the other does not.

I pass the the object into an error handle method where I log the properties into a text file.
Looking at the Watch window it seems that the Execption does not have System.Reflection etc...

Regards,

Joe


"Robbe Morris - [MVP] C# said:
> System.Reflection and iterate through the PropertyInfo[].
>
> The Type of the object has a .GetProperties method.
>
> --
> Robbe Morris [Microsoft MVP - Visual C#]
> .NET Setup Deployment - MSI, Cassini, SQL Server, NTFS
> http://www.eggheadcafe.com/tutorial...5ab-35ca33da0f65/net-setup-deployment--m.aspx
>
>
>
> "JoeP said:
>> Hi All,
>>
>> Need to check if the propery of an object exits.
>>
>> oMyObject.Message
>>
>> In some cases the Message property may not exists. I know the GetType()
>> can do it.
>> How would you check it?
>>
>> Thanks,
>>
>> Joe
>>
>
 

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

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top