Tip: Windows Phone - 匿名类型和 MethodAccessException

一个wp7应用引用了Facebook csharp sdk 的dll, 传给FacebookClient的方法中使用了一个匿名类型,结果抛出MethodAccessException

使用匿名类型:
fb.PostAsync("oauth/access_token",new
                                                          {
                                                              client_id = AppId,
                                                              client_secret = AppSecret,
                                                              redirect_uri = RedirectUri,
                                                              code = oauthResult.Code
                                                          });
异常代码:

 foreach (var propertyInfo in parameters.GetType().GetProperties())
                {
                    if (!propertyInfo.CanRead) continue;
                    dictionary[propertyInfo.Name] = propertyInfo.GetValue(parameters, null);
                }

异常原因: 匿名类型被编译为internal成员,跨程序集访问时会遇到此异常。

解决办法:可在使用匿名类型的程序集AssemblyInfo.cs中使用如下特性:

//make anonymous type in this assembly can be accessible by Facebook.dll
[assemblyInternalsVisibleTo("Facebook")]

参考: 

http://stackoverflow.com/questions/8273399/anonymous-types-and-get-accessors-on-wp7-1

http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute.aspx


你可能感兴趣的:(windows,assembly,Parameters,token,Facebook,redirect,phone)