在 Windows Phone 上使用 F# sprintf "%A" 时出现反射错误

Gus*_*rra 5 silverlight f# windows-phone

我有一组这样的 F# 记录类型:

type Course =
    { Id : int
      Title : string
      Instructor : string
      Duration : string
      StartDate : string
      IconUrl : string
      Url : string 
      LectureSections : LectureSection list }

and LectureSection =
    { Title : string
      Completed : bool
      Lectures : Lecture list }

and Lecture = 
    { Title : string
      VideoUrl : string }
Run Code Online (Sandbox Code Playgroud)

在某些时候我打电话

sprintf "%A" course
Run Code Online (Sandbox Code Playgroud)

其中 course 是 Course 记录的一个实例

在常规 .NET 项目中,这工作正常,但在 Windows Phone 7.1/Silverlight 4 F# 项目(我使用的是 Daniel Mohl 的模板)上,我收到此错误:

Late bound operations cannot be performed on types or methods for which ContainsGenericParameters is true.
Run Code Online (Sandbox Code Playgroud)

问题似乎是列表。有谁知道解决这个问题的任何方法?

Joh*_*Joh 2

这些模板应附带自定义构建的 FSharp.Core.dll,用于禁用 Windows Phone 上不可用的功能。您确定您正在针对此 dll 进行编译,而不是针对 Windows PC 进行编译吗?

我在使用 Xbox360 和 XNA 时也遇到过类似的问题。F# 团队向我发送了一个适合 Xbox360 使用的 dll,以及有关用于构建该 dll 的设置的一些简短说明。

这是我们用来编译 FSharp.Core 的属性组:

 <PropertyGroup Condition="'$(TargetFramework)'=='Xbox360\CompactFramework\3.7'">
   <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
   <TargetFrameworkProfile>Client</TargetFrameworkProfile>
   <XnaFrameworkVersion>v4.0</XnaFrameworkVersion>
   <XnaPlatform>Xbox 360</XnaPlatform>
   <XnaProfile>HiDef</XnaProfile>
   <XnaCrossPlatformGroupID>a8d70e6b-9a75-4aec-80f8-62cf373f7368</XnaCrossPlatformGroupID>
   <XnaOutputType>Game</XnaOutputType>
   <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
   <DefineConstants>$(DefineConstants);FX_NO_ARRAY_LONG_LENGTH;FX_NO_DEBUG_PROXIES;FX_NO_EXIT;FX_FSLIB_IOBSERVABLE;FX_NO_WEB_CLIENT;FX_NO_WEB_REQUESTS;FX_NO_CHAR_PARSE;FX_NO_DEFAULT_DEPENDENCY_TYPE;FX_SIMPLE_SECURITY_PERMISSIONS;FX_NO_TRUNCATE;FX_NO_CULTURE_INFO_ARGS;FX_NO_REFLECTION_MODULE_HANDLES;FX_NO_OPERATION_CANCELLED;FX_NO_TO_LOWER_INVARIANT;FX_NO_EXIT_CONTEXT_FLAGS;FX_NO_BASED_ARRAYS;FX_NO_DOUBLE_BIT_CONVERTER;FX_NO_BINARY_SERIALIZATION;FX_NO_ASCII_ENCODING;FX_NO_DEFAULT_ENCODING;FX_NO_FILE_OPTIONS;FX_NO_NONBLOCK_IO;FX_NO_COMMAND_LINE_ARGS;FX_NO_ENVIRONMENT;FX_NO_PROCESS_START;FX_NO_APP_DOMAINS;FX_NO_PROCESS_DIAGNOSTICS;FX_FSLIB_STRUCTURAL_EQUALITY;FX_FSLIB_LAZY;FX_FSLIB_TUPLE;FX_NO_REFLECTION_EMIT</DefineConstants>
   <Tailcalls>false</Tailcalls>
   <!-- It would be better to use MSBuild resolution here, but the TargetFrameworkIdentifier etc. aren't set up quite correctly as yet -->
   <OtherFlags>$(OtherFlags) --simpleresolution -r:"C:\Program Files\Microsoft XNA\XNA Game Studio\v4.0\References\Xbox360\mscorlib.dll"</OtherFlags>
 </PropertyGroup>
Run Code Online (Sandbox Code Playgroud)

以及我们使用的新 .targets:

<Import Project="$(MSBuildExtensionsPath)\Microsoft\XNA Game Studio\Microsoft.Xna.GameStudio.targets" Condition="'$(TargetFramework)'=='Xbox360\CompactFramework\3.7'"/>
Run Code Online (Sandbox Code Playgroud)

他们发送给我的 dll 工作正常,我从来不需要使用这些指令,但它们可能对想要为新平台构建 FSharp.Core.dll 的人有用。特别注意该DefineConstants部分。