G
Guest
Hello, friends,
In the following C# code, I tried to get the values for each property in an
object:
public void WriteObjectValue(object obj)
{
Type objectType = obj.GetType();
PropertyInfo[] properties = objectType.GetProperties();
foreach (PropertyInfo property in properties)
{
System.Object propertyValue = property.GetValue(obj, null);
Console.WriteLine(propertyValue.ToString());
}
}
This worked fine, except in the following situation:
If the passed object obj was defined like the follows
Public class IntObjClass
{
int? deptId;
int? sectionId;
public int DeptId {set {deptId = value;}}
public int SectioinId {set {sectionId = value;}}
}
and only sectionId was assigned a value, say 100, while deptId was left null.
At this situation, the foreach loop will throw an exception at
System.Object propertyValue = property.GetValue(),
complaining deptId was null.
Is there a way to get around this? (I don't want to use try{} catch {} to
handle this exception)
Thanks a lot for your help.
In the following C# code, I tried to get the values for each property in an
object:
public void WriteObjectValue(object obj)
{
Type objectType = obj.GetType();
PropertyInfo[] properties = objectType.GetProperties();
foreach (PropertyInfo property in properties)
{
System.Object propertyValue = property.GetValue(obj, null);
Console.WriteLine(propertyValue.ToString());
}
}
This worked fine, except in the following situation:
If the passed object obj was defined like the follows
Public class IntObjClass
{
int? deptId;
int? sectionId;
public int DeptId {set {deptId = value;}}
public int SectioinId {set {sectionId = value;}}
}
and only sectionId was assigned a value, say 100, while deptId was left null.
At this situation, the foreach loop will throw an exception at
System.Object propertyValue = property.GetValue(),
complaining deptId was null.
Is there a way to get around this? (I don't want to use try{} catch {} to
handle this exception)
Thanks a lot for your help.