
我制作了一個名為 DraggableLayout 的通用 UI 類。這是一個嵌套在其他 DraggableLayout 中的樹狀布局
問題是,當我回圈瀏覽父母中的控制元件串列時,我需要孩子們能夠搜索 DraggableLayout。
foreach (var tab in Parents)
{
if (tab is DraggableLayout) //Error CS0305
{
//Do Something
}
}
當我執行上述操作時,我需要指定型別。是否可以忽略并搜索父泛型類?
uj5u.com熱心網友回復:
你可以做的是:
var tabType = tab.GetType();
if (tabType.IsGenericType
&& typeof(DraggableLayout<>)
.IsAssignableFrom(tabType.GetGenericTypeDefinition()))
{ ... }
或者使用擴展方法:
public static bool IsOfGenericType(this Type source, Type genericType)
{
return source.IsGenericType
&& genericType.IsAssignableFrom(source.GetGenericTypeDefinition());
}
// ...
if (typeof(tab).IsOfGenericType(typeof(DraggableLayout<>)))
{ ... }
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/504304.html
上一篇:如何減少mvc中資料庫的價值?
