Reflection - Transform a dotnet component to XML

A

Ahab Guirguis

hello,

I receive an error on the recursive call...... The text colored blue is my center of the problem....
The funuction defined below works fine with the exception of sub classes. when the function reaches a sub-class like fonts property or text the function fails with the error

Parameter count mismatch.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Reflection.TargetParameterCountException: Parameter count mismatch.


Can any one see a problem with this code
Thanks in advance....


string xml = ToXmlProperties(new TextBox()
txtXml.Text = xml;
//--------------------------------------------------------------------------
------
public string ToXmlProperties(Object obj)
{
string xml = String.Empty;
Type t = obj.GetType();
string propertyValue = String.Empty;
xml = "<properties>";
PropertyInfo [] pi = t.GetProperties(BindingFlags.Instance |
BindingFlags.NonPublic | BindingFlags.Public);
try
{
foreach (PropertyInfo p in pi)
{
xml += "<property>";
xml += "<name>" + p.Name.ToString() + "</name>";
xml += "<PropertyType>" + p.PropertyType.ToString() + "</PropertyType>";
xml += "<IsClass>" + p.PropertyType.IsClass.ToString() + "</IsClass>";
if ((p.PropertyType.IsClass))
{
object objProperty = p.GetValue(obj, Type.EmptyTypes); /* this fails on the sub objects - example fonts... */
propertyValue = ToXmlProperties(objProperty);
}
else
{
propertyValue = p.GetValue(obj, Type.EmptyTypes).ToString();
}
xml += "<value>" + propertyValue + "</value>";
xml += "</property>";
}
}
catch (System.NullReferenceException ex) {}
xml += "</properties>";
return xml;
}
//--------------------------------------------------------------------
 
T

Tian Min Huang

Hello Ahab,

Thanks for your post. I reviewed your description carefully, and think more
information is needed before moving forward:

Could you please tell me the content of call stack when the exception is
thrown?

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
M

Mattias Sjögren

Ahab,

Sounds like you're hitting a parameterized property (an indexer fro
example). You can check p.GetIndexParameters().Length and act
accordingly.



Mattias
 
T

Tian Min Huang

Hi Ahab,

I checked your code on my side. Mattias is correct that the exception is
throw when you call PropertyInfo.GetValue against the indexed properties.
Please refer to the following:

//------------------code snippet------------------
if(p.GetIndexParameters().Length ==0)
propertyValue = p.GetValue(obj, Type.EmptyTypes).ToString();
else
{
// for indexed property, you should provide index when calling
PropertyInfo.GetValue()
......
}
//-----------------end of------------------

Hope this helps.

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

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

Top