【发布时间】:2013-10-25 06:55:21
【问题描述】:
我在 VS 2012 中为 Windows 8 应用商店应用程序创建了一个类库项目 (dll),但现在引入了 Windows 8.1,有一些新的 API 可用于我想要的操作系统(例如广告 ID 以唯一标识用户)在我的 dll 中使用,但我不想发布针对 Windows 8.1 的单独 dll。我的目标是分发可以在 Windows 8 和 Windows 8.1 商店应用程序中引用的单个 dll。如果我将创建一个针对 8.1 的 dll,那么 8.0 应用程序将无法使用我的 dll。
有没有办法检查运行时可用的特定 Api 或 Windows 8.1 应用程序的任何预处理器,以便我的 dll 在运行时识别操作系统并执行代码,例如
string deviceId=string.Empty;
#if W8.1
deviceId=Windows.System.UserProfile.AdvertisingManager.AdvertisingId;
#endif
或者请建议任何其他方式,以便我只能将一个 dll 分发给我的用户?
【问题讨论】:
-
你可以使用version helper APIs
-
假设您可以在 Windows 应用程序中进行反射,您可以通过反射来实现吗? (但我不明白为什么不)按名称访问类和属性
-
我正在尝试使用 Type 查找 API 可用性,但
Type tp = Type.GetType("Windows.System.UserProfile.AdvertisingManager");返回 null。但是当我使用Type tp1 = typeof(Windows.System.UserProfile.AdvertisingManager);它返回我的类但它会给我在 VS2012 中的编译错误,因为这个 api 不可用 -
我犯了这个错误。
Type.GetType需要完全限定名称。正确的代码是:Type tp = Type.GetType("Windows.System.UserProfile.AdvertisingManager, Windows.System, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime"); if (tp != null) { PropertyInfo properties = tp.GetRuntimeProperty("AdvertisingId"); if (properties != null) { string deviceId = (string)properties.GetValue(null); } }
标签: c#-4.0 visual-studio-2012 dll windows-store-apps