便携式类库和反思 [英] Portable Class library and reflection

查看:138
本文介绍了便携式类库和反思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在同一时间建设桌面,Windows 8商店和Windows Phone新应用程序。所以我创建便携式类库有跨所有平台通用的功能。我的问题是,当我尝试重用我的PCL里面的代码,我不能访问内库的一些方法和属性。根据MSDN这些方法都支持,但我现在知道我为什么不能访问它们。

I am building new application for Desktop, Windows 8 store and Windows phone at the same time. so I created Portable Class library to have common functionality across all platforms. my problem is that when I try to reuse my code inside PCL I can not access some methods and properties inside library. According to MSDN those methods are supported but I do know now why I can not access them.

        var property = memberExpression.Member as PropertyInfo;
        if (property == null)
        {
        }

        var getMethod = property.GetGetMethod(true);
        if (getMethod.IsStatic)
        {}

下面是片段不能被编译的代码。 GetGetMethod和IsStatic在里面红色的Visual Studio编辑器。我不知道这是为什么发生以及如何访问这些属性。

here is the fragment of the code that can not be compiled. GetGetMethod and IsStatic are in red inside Visual Studio editor. I have no idea why is that happening and how to access those properties.

所以请,如果有任何人曾经做过类似的东西,帮我做这个代码编译

so please if anyone out there has ever done something like that, help me to make this code compile.

推荐答案

我们的确在反射API,用于.NET的Windows Store应用程序的一些重构。请参阅博客文章进化反射API 了解详情。除其他事项外,该API的变化陷害我们在未来更好的便携性。新的API是在Windows Store应用程序可用,.NET 4.5和Windows Phone 8,出于兼容性考虑,旧的API当然是在.NET 4.5和Windows Phone 8仍然可用。

We did some refactoring in the reflection APIs for .NET for Windows Store apps. See the blog post Evolving the Reflection API for details. Among other things, the API changes set us up for better portability in the future. The new APIs are available in Windows Store apps, .NET 4.5 and Windows Phone 8. For compatibility, the old APIs are of course still available on .NET 4.5 and Windows Phone 8.

有关便携式类库,如果您指定的只有的其中新的反射API都支持的平台,那么你将只能得到新的API。如果您添加不支持新的API,那么你将得到的API的平台。

For Portable Class Libraries, if you target only platforms where the new reflection APIs are supported, then you will get only the new APIs. If you add a platform that doesn't support the new APIs then you will get the APIs.

PropertyInfo.GetGetMethod()不是新的API的一部分,所以你应该使用 PropertyInfo.GetMethod 来代替。 MethodInfo.IsStatic 是新的API的一部分,你看到红色的波浪线在Visual Studio中出现了,因为它不知道getMethod是什么类型的,因为你使用<$ C的原因。$ C> VAR 和GetGetMethod()无法识别

PropertyInfo.GetGetMethod() isn't part of the new APIs, so you should use PropertyInfo.GetMethod instead. MethodInfo.IsStatic is part of the new APIs, the reason you saw red squiggles in Visual Studio there was because it didn't know what type getMethod was because you used var and GetGetMethod() wasn't recognized.

所以,你的代码应该是这个样子:

So, your code should look something like this:

    var property = memberExpression.Member as PropertyInfo;
    if (property == null)
    {
    }

    var getMethod = property.GetMethod;
    if (getMethod != null && getMethod.IsStatic)
    {}

这篇关于便携式类库和反思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆