在Visual Studio 2013中,我的一个项目包括:
<ItemGroup>
<Compile Include="Entity\Abstract\Entity.fs" />
<Compile Include="Entity\HumanEntity.fs" />
<Compile Include="State\Abstract\State.fs" />
<Compile Include="State\Abstract\HumanState.fs" />
<Compile Include="State\Human\HumanIdleState.fs" />
<Compile Include="State\Human\HumanAwakenState.fs" />
</ItemGroup>
Run Code Online (Sandbox Code Playgroud)
Visual Studio对此嗤之以鼻,声称:
The project 'Entity.fsproj' could not be opened because opening it would cause a folder to be rendered multiple times in the solution explorer. One such problematic item is 'State\Abstract\State.fs'.
如果我像这样更改包含,一切都很好:
<ItemGroup>
<Compile Include="Entity\AbstractEntity\Entity.fs" />
<Compile Include="Entity\HumanEntity.fs" />
<Compile Include="State\AbstractState\State.fs" />
<Compile Include="State\AbstractState\HumanState.fs" />
<Compile Include="State\Human\HumanIdleState.fs" />
<Compile Include="State\Human\HumanAwakenState.fs" />
</ItemGroup>
Run Code Online (Sandbox Code Playgroud)
这是VS2013的疏忽,还是我做错了什么?
如果我采用定义类型的函数方法,然后描述对该类型进行操作的函数(而不是实例方法),我应该如何组织我的代码?
我通常会选择以下三种方式之一:
(1):
module MyType =
type MyType (x : int) =
member val X = x with get
let myFunction myType y = myType.X + y
Run Code Online (Sandbox Code Playgroud)
(2):
type MyType (x : int) =
member val X = x with get
[<RequireQualifiedAccess>]
module MyTypeOps =
let myFunction myType y = myType.X + y
Run Code Online (Sandbox Code Playgroud)
(3):
type MyType =
{
x : int
}
static member MyFunction myType y = myType.x + y
Run Code Online (Sandbox Code Playgroud)
(1)的Pro是函数在模块中定义.(1)的缺点是模型中也定义了类型,导致MyType.MyType实例化时出现难看的冗余,[<RequireQualifiedAccess>]如果您想要允许open MyType作为冗余的解决方案,则无法实现.
(2)的优点是模块中定义的函数,没有module.type冗余.Con是模块不能与类型同名. …
我有一个带泛型参数的函数,在其中我需要执行两个函数中的一个,具体取决于参数的类型.
member this.Load<'T> _path =
let hhType = typeof<HooHah>
match typeof<'T> with
| hhType -> this.LoadLikeCrazy<'T> _path
| _ -> this.LoadWithPizzaz<'T> _path
Run Code Online (Sandbox Code Playgroud)
....其中LoadLikeCrazy和LoadWithPizzaz都返回'T.
VS通知我,通配符的情况永远不会被执行,因为我显然在编译时得到泛型的类型,而不是运行时的实际类型.我该怎么做?
我有一个Dictionary我最初迭代,因此:
myDictionary |> Seq.iter (fun kvp -> doSomething kvp.Key kvp.Value)
后来,我发现我可以使用KeyValue活动模式,并执行此操作:
myDictionary |> Seq.iter (fun (KeyValue (k, v)) -> doSomething k v)
知道活动模式不是某种形式的预处理器指令,我如何能够kvp将lambda中的参数替换为分解它的函数?
这有效:
type MyType () =
static member MyFn (fn : Func<bool>) = fn.Invoke ()
MyType.MyFn (fun _ -> false)
Run Code Online (Sandbox Code Playgroud)
这不(错误FS0002):
let myFn (fn : Func<bool>) = fn.Invoke ()
myFn (fun _ -> false)
Run Code Online (Sandbox Code Playgroud)
这也不是(错误FS0002):
type MyDU = Fn of Func<bool>
Fn (fun _ -> false)
Run Code Online (Sandbox Code Playgroud)
这种相当恼人的不一致的原因是什么?
给出以下代码:
type MyDU =
| B of bool
| I of int
| S of string
type Ops () =
static member myFn<'T> x =
let v =
match x with
| B b -> box b
| I i -> box i
| S s -> box s
match v with
| :? 'T as y -> Some y
| _ -> None
Run Code Online (Sandbox Code Playgroud)
......以下产品error FS0717: Unexpected type arguments:
I 7 |> Ops.myFn<int>
Ops.myFn<int> <| I 7
但是,这很好用: …
在运行 Visual Studio Pro 2013 12.0.21005.1 的构建计算机上,我有一个单元测试项目无法正确读取其 App.config 文件(如果有的话)。以下返回 null:
System.Configuration.ConfigurationManager.GetSection( "loggingConfiguration" )
不仅仅是日志配置部分无法读取;还包括日志配置部分。任何读取任何部分的尝试都会导致空值。
在运行 Visual Studio Pro 2013 12.0.31101.00 Update 4 的笔记本电脑上,单元测试项目可以很好地读取 App.config,并且上面的调用返回预期的对象。
我的 XML 看起来像这样:
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
<section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" />
<section name="dataCacheClient" type="Microsoft.ApplicationServer.Caching.DataCacheClientSection, Microsoft.ApplicationServer.Caching.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
</configSections>
<loggingConfiguration name="" tracingEnabled="false" defaultCategory="Verbose" logWarningsWhenNoCategoriesMatch="false">
<listeners>
<add name="Event Log Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, …Run Code Online (Sandbox Code Playgroud) c# unit-testing app-config enterprise-library-5 visual-studio-2013
给出以下C#代码:
var dt = DateTime.Now;
Console.WriteLine("{0:MM/dd/yy} ... {1}", dt, string.Format("{0:MM/dd/yy}", dt));
Run Code Online (Sandbox Code Playgroud)
...当短日期(在Windows 7下Control Panel -> Region and Language -> Additonal Settings -> Date)设置为美国标准" M/d/yyyy,"时,我得到:
06/17/14 ... 06/17/14
Run Code Online (Sandbox Code Playgroud)
但是,当我将短日期更改为" ddd dd MMM yyyy,"时,我得到了这个:
06/17/14 ... 06 17 14
Run Code Online (Sandbox Code Playgroud)
我的印象是,Console.WriteLine并且string.Format始终将字符串格式化的DateTime值相同.这种差异的解释是什么?
编辑:看起来这只发生在标准的单元测试输出(Visual Studio),这是我最初看到的问题.当代码在控制台应用程序中执行时,输出为06 17 14 ... 06 17 14.
以下产量
此构造使代码不如类型注释所指示的那样通用.类型变量'P已被约束为类型'bool'.
对于let myValue =表达的右侧,和
此代码的通用性低于其注释所需的代码,因为显式类型变量"P"无法进行泛化.它被限制为'bool'.
对于通用<'P>的Value方法:
type MyTypeA<'T> (myObject : 'T) as this =
let myValue = this.Value<bool> "SomeBooleanProperty"
member this.Value<'P> name = typeof<'T>.GetProperty(name, typeof<'P>).GetValue(myObject, null) :?> 'P`
Run Code Online (Sandbox Code Playgroud)
但是,这编译得很好,不会产生任何警告或错误:
type MyTypeB<'T> (myObject : 'T) as this =
member this.Value<'P> name = typeof<'T>.GetProperty(name, typeof<'P>).GetValue(myObject, null) :?> 'P
member this.Method<'P> name = this.Value<'P> name
Run Code Online (Sandbox Code Playgroud)
这里发生了什么?在第一个示例中,为什么在私有值的赋值中识别的方法,而不是合法的通用方法?
目前,我正在使用Daniel Mohl的"F#Web应用程序(Silverlight)"扩展来完全使用F#(Visual Studio 2010)创建Silverlight应用程序.
我需要做本地文件I/O(这显然需要提升信任度),我也想在浏览器外运行应用程序,但我无法以正常方式设置它(例如正如我在Daniel Mohl的"F#C#Web App(Silverlight)"扩展中所做的那样,因为全F#配置中的项目设置不提供必要的设置.
我可以通过在文本编辑器中对项目文件进行一些简单的修改来设置它,还是涉及更多步骤?
f# ×8
c# ×2
generics ×2
types ×2
.net ×1
app-config ×1
datetime ×1
function ×1
module ×1
naming ×1
project ×1
silverlight ×1
unit-testing ×1