看板 C_Sharp 關於我們 聯絡資訊
好吧...我知道這很蠢.. public class father { public void FatherFunction() { Console.WriteLine("this is father"); } } public class child : father { public void ChileFunction() { Console.WriteLine("this is child"); } } List<father> fatherCol = new List<father>() { new father(), new child()...brabra... }; foreach(var tar in fatherCol) { dynamic d = tar; switch(tar.GetType().Name) { case "father": d.FatherFunction(); break; case "child": d.ChileFunction(); break; } } ※ 引述《grace0523 (小小)》之銘言: : Class father : {} : Class child : father : {} : father f = new child(); : Child C1 = (child)f ;//第一種強轉 : child C2 = f as child ;//第二種強轉 : 除了上述兩種強轉外 : 還有什麼方式可以轉成子類別呢 : 有沒有動態的方式能轉呢 : 例如有多種子類別實體 : 放在父類別的List變數裡 : 要怎麼依序的將該List轉成子類別呢 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 123.110.33.129 ※ 文章網址: https://www.ptt.cc/bbs/C_Sharp/M.1448295625.A.3F8.html