我有一個 Blazor 服務,它接受任何可拖動的物件,裝箱為object
. 它可以是一個被拖拽的用戶,或者一個教室,或者一個預定的專案,或者幾乎任何我以后可能想到的東西。
當 draggable 被放入任何支持放置的組件時,該組件需要檢查它是否是正確的物件型別。例如,StudentList.razor 組件將只接受掉落,如果它們是IdentityUser
或(IdentityUser, string)
字串可能是角色名稱或其他一些任意資訊的雙元組(稍后待定):
<div class="class-students-drop" @ondrop="_=>HandleStudentDrop()">
. . .
</div>
@code {
async Task HandleStudentDrop()
{
if (DM.GetItem() is IdentityUser Person)
{
// Do generic user thing (works fine)
}
if (DM.GetItem() is (IdentityUser person,string role) RolePerson)
{
// Do thing based on specified role
// Error (active) CS1061 'object' does not contain a definition for 'Deconstruct' and no accessible extension method 'Deconstruct' accepting a first argument of type 'object' could be found
}
}
}
我可以對類實體進行模式檢查,例如IdentityUser
,但我不知道如何檢查裝箱物件是否適合特定的雙重形式。
我的問題:使用“is”關鍵字檢查雙重簽名的正確語法(如果有的話)是什么?
我見過使用 值進行模式匹配雙元組的示例switch
,但我真的只想檢查裝箱物件是否為 `(IdentityUser, string) 雙元組。
我的參考:
在你的情況下,它是:
async Task HandleStudentDrop()
{
if (DM.GetItem() is IdentityUser Person)
{
// Do generic user thing (works fine)
}
else if (DM.GetItem().IsTuple( out ( IdentityUser person, String role )? ok ) )
{
<p><b>Name:</b> @( ok.Value.person.Name )</p>
<p><b>Role:</b> @( ok.Value.role )</p>
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/537940.html
標籤:C#开拓者
下一篇:如何在流程時呼叫活動指示器