Rat*_*_Ge 11 c# reflection portable-class-library
我正在为桌面,Windows 8商店和Windows手机同时构建新的应用程序.所以我创建了可移植类库,以便在所有平台上具有通用功能.我的问题是,当我尝试在PCL中重用我的代码时,我无法访问库中的一些方法和属性.根据MSDN,支持这些方法,但我现在知道为什么我无法访问它们.
var property = memberExpression.Member as PropertyInfo;
if (property == null)
{
}
var getMethod = property.GetGetMethod(true);
if (getMethod.IsStatic)
{}
Run Code Online (Sandbox Code Playgroud)
这是无法编译的代码片段.GetGetMethod和IsStatic在Visual Studio编辑器中是红色的.我不知道为什么会发生这种情况以及如何访问这些属性.
所以,如果那里的任何人做过类似的事情,请帮我编译这段代码.
Dan*_*ted 18
我们在.NET for Windows Store应用程序的反射API中进行了一些重构.有关详细信息,请参阅博客文章Evolving the Reflection API.除此之外,API更改为我们提供了更好的可移植性.新的API在Windows应用商店应用程序,.NET 4.5和Windows Phone 8中可用.为了兼容性,旧的API当然仍然可以在.NET 4.5和Windows Phone 8上使用.
对于可移植类库,如果您仅定位支持新反射API的平台,那么您将只获得新的API.如果您添加的平台不支持新API,那么您将获得API.
PropertyInfo.GetGetMethod()不是新API的一部分,所以你应该使用它PropertyInfo.GetMethod. MethodInfo.IsStatic是新API的一部分,你在Visual Studio中看到红色曲线的原因是因为它不知道getMethod是什么类型,因为你使用了varGetGetMethod()并且无法识别.
所以,你的代码应该是这样的:
var property = memberExpression.Member as PropertyInfo;
if (property == null)
{
}
var getMethod = property.GetMethod;
if (getMethod != null && getMethod.IsStatic)
{}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10191 次 |
| 最近记录: |