Wiki Link: [discussion:2798]
SL4 methodaccessexception over TypedParameterExpression 

Jan 8 2010 at 2:26 AM
Hi,

I just converted a silverlight project to version 4 SL. Iam trying to serialize expressions over the wire using WebRequest/Response objects and it all worked perfectly till I juist upgraded to SL4. Here's the method that is throwing the exception

private XElement GenerateXmlFromExpressionCore(Expression e)
{
if (e == null)
return null;
XElement replace = ApplyCustomConverters(e);
if (replace != null)
return replace;
return new XElement(
GetNameOfExpression(e),
from prop in e.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)
select GenerateXmlFromProperty(prop.PropertyType, prop.Name, prop.GetValue(e, null)));
}

and prop.GetValue causes a MethodAccessException while trying to iterating through TypedParameterExpression Type.

Please, do let me know what has changed and what needs to be done in order for the expressionserializer to work in SL4.

Thanks,
Junaid

Apr 23 2010 at 6:28 AM
Hi Juniad,

I've customised this thing in about the same direction and do have the same problem now. I'm in the middle of fixing it right now. I'll post a solution here once I get over it.

Jun 6 2010 at 2:40 AM
Hi Juniad,

the solution I found is a bit 'hard coded', but better than nothing

private XElement GenerateXmlFromExpressionCore(Expression Expression)
{
if (Expression == null) return null;
XElement replace = this.ApplyCustomConverters(Expression);
if (replace != null) return replace;
List<object> list = new List<object>();

foreach (PropertyInfo p in Expression.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
try
{
object value = p.GetValue(Expression, null);

if (IsString(p))
{
if (value != null)
{
object item = this.GenerateXmlFromProperty(p.PropertyType, p.Name, value);
list.Add(item);
}
}
else
{
object item = this.GenerateXmlFromProperty(p.PropertyType, p.Name, value);
list.Add(item);
}
}
catch (MethodAccessException)
{
if (p.Name == "Type")
{
object item = this.GenerateXmlFromProperty(p.PropertyType, p.Name, Expression.Type);
list.Add(item);
}
else if (p.Name == "NodeType")
{
object item = this.GenerateXmlFromProperty(p.PropertyType, p.Name, Expression.NodeType);
list.Add(item);
}
else
{
Debug.WriteLine("Can't access property: " + p.Name);
}
}
catch
{
Debug.WriteLine("Exception");
}
}

return new XElement(this.GetNameOfExpression(Expression), list);
}

private bool IsString(PropertyInfo PropertyInfo)
{
return (PropertyInfo.PropertyType.Equals(typeof(string)));
}


Updating...
Page view tracker