Need to check if the propery of an object exits.

  • Thread starter Thread starter JoeP
  • Start date Start date
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
 
System.Reflection and iterate through the PropertyInfo[].

The Type of the object has a .GetProperties method.
 
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
 
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
 
Back
Top