C# 4.0 适用于Windows 8和Windows 8.1的单个dll
我已经在VS 2012中为Windows 8应用商店应用程序创建了一个类库项目(dll),但现在Windows 8.1的推出为操作系统提供了一些新的API(例如,用于唯一标识用户的广告ID),我想在我的dll中使用这些API,但我不想发布针对Windows 8.1的单独dll。我的目标是分发一个可以在Windows 8和Windows 8.1应用商店应用程序中引用的dll。如果我将创建一个针对8.1的dll,那么8.0应用程序将无法使用我的dll 是否有任何方法可以检查运行时可用的特定Api或Windows 8.1应用程序的任何预处理器,以便我的dll在运行时识别操作系统并执行代码,例如C# 4.0 适用于Windows 8和Windows 8.1的单个dll,c#-4.0,visual-studio-2012,dll,windows-store-apps,C# 4.0,Visual Studio 2012,Dll,Windows Store Apps,我已经在VS 2012中为Windows 8应用商店应用程序创建了一个类库项目(dll),但现在Windows 8.1的推出为操作系统提供了一些新的API(例如,用于唯一标识用户的广告ID),我想在我的dll中使用这些API,但我不想发布针对Windows 8.1的单独dll。我的目标是分发一个可以在Windows 8和Windows 8.1应用商店应用程序中引用的dll。如果我将创建一个针对8.1的dll,那么8.0应用程序将无法使用我的dll 是否有任何方法可以检查运行时可用的特定Api或W
string deviceId=string.Empty;
#if W8.1
deviceId=Windows.System.UserProfile.AdvertisingManager.AdvertisingId;
#endif
或者请建议其他方法,以便我只能将一个dll分发给我的用户 您可以尝试基于.NET framework版本创建自己的条件指令。Windows 8.1使用.NET 4.5.1
此MSDN示例可能会帮助您=>最终通过反射来完成。AdvertisingManager API在Windows 8中不可用,但如果应用程序在Windows 8.1上运行,则同一dll(目标框架为Windows 8)将通过反射访问AdvertisingManager。因此,无需为不同版本分发两个dll
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);
}
}
输出
案例1:在Windows 8上运行的Windows 8应用程序
在这种情况下,tp将返回null,因为AdvertisingManager API在Windows 8中不可用
案例2:在Windows 8.1上运行的Windows 8应用程序
由于AdvertisingManager API在Windows 8.1中可用,所有针对Windows 8的应用程序都可以访问此API,在这种情况下将获得AdvertisingId
案例3:在Windows 8.1上运行的Windows 8.1应用程序
该API可直接在Windows 8.1应用程序中使用。因此,无需通过反射路径 如果允许在Windows应用程序中使用反射,您可以使用“您可以通过反射来完成”吗?(但我看不出为什么不)要按名称访问类和属性,我正在尝试使用Type但
Type tp=Type.GetType(“Windows.System.UserProfile.advisingmanager”)查找API可用性代码>返回null。但是当我使用typetp1=typeof(Windows.System.UserProfile.advisingmanager)时代码>它返回我的类,但在VS2012中它会给我编译错误,因为此api不可用。我犯了错误<代码>类型。GetType
需要完全限定的名称。正确的代码是:Type tp=Type.GetType(“Windows.System.UserProfile.advisingmanager,Windows.System,Version=255.255.255,Culture=neutral,PublicKeyToken=null,ContentType=WindowsRuntime”);如果(tp!=null){PropertyInfo properties=tp.GetRuntimeProperty(“AdvertisingId”);如果(properties!=null){string deviceId=(string)properties.GetValue(null);}}}
我认为这会在编译时设置目标框架,这不是他想要的。