IsAssignableFrom or AS?
问题 I have next code: private T CreateInstance<T>(object obj) // where T : ISomeInterface, class { ... if (!typeof(T).IsAssignableFrom(obj.GetType())) { throw ..; } return (T)obj; } Can it be replaced with this: T result = obj as T; if (result == null) { throw ..; } return result; If not - why? 回答1: Another variant: private T CreateInstance<T>(object obj) where T : ISomeInterface // as OP mentioned above { ... T result = obj as T; if (result == null) { throw ..; } else return result; } 回答2: What