【发布时间】:2010-09-03 07:17:04
【问题描述】:
我在 VS 2010 中使用 Silverlight 4 并尝试对匿名类型进行反射,但我得到了一些“尝试通过方法 '...' 访问方法 '...' 失败。”。我为此尝试了各种解决方法,但找不到简单的解决方法。
class.CallAnonymous("SimpleClass", "HelloFunc", new { strIn = "Boo" });
public void CallAnonymous(string cName, string cAction, object anonymousParms)
{
Type anonymousType = anonymousParms.GetType();
PropertyInfo[] props = anonymousType.GetProperties();
ServiceParam serviceParam = new ServiceParam();
foreach (var info in props)
{
string propertyName = info.Name;
object propertyObj = info.GetValue(anonymousParms, null);
// Throw the exception on PropertyInfo.GetValue()
serviceParam.Add(propertyName, propertyObj);
}
}
【问题讨论】:
标签: c# silverlight reflection anonymous-types propertyinfo