static void DoWithSomeType(object obj) { //SecondType secondType = (SecondType)obj; SecondType secondType = obj as SecondType; if (secondType != null) { // do someting } }
static void Main(string[] args) { FirstType firstType = new FirstType() { Name = "First Type" }; SecondType secondType = (SecondType)firstType; //转型成功 //secondType = firstType as SecondType; //编译期转型失败,编译不通过 Console.Read(); }