2015-10-19 21 views
0

我在C#中有以下代碼。它運作良好。無法在F中將IIdentity轉換爲WindowsIdentity#

WindowsIdentity winId = (WindowsIdentity)HttpContext.Current.User.Identity; 
WindowsImpersonationContext ctx = winId.Impersonate(); 
ctx.Undo(); 

但我想轉換F#代碼。我寫了下面的代碼。

Let winId = HttpContext.Current.User.Identity :> WindowsIdentity //Showing error 
Let ctx = winId.Impersonate() 
ctx.Undo() 

在上面的代碼HttpContext.Current.User.Identity不在WindowsIdentity能夠鑄造。請讓我知道我該怎麼做。

回答

7

的問題是,您使用的向上轉型(:>)操作時要使用的向下轉換(:?>)運算符。上傳是在編譯時檢查的,因爲您始終可以知道是否可以強制繼承層次結構,但是強制轉換層次結構可能會失敗,因此它有一個單獨的運算符,可能會在運行時失敗。