The code sent to get the most specific common base for a set of types has some problems. In particular, it breaks when I pass typeof (object) as one of the types. I believe the following is simpler and (better) correct.
public static Type GetCommonBaseClass (params Type[] types) { if (types.Length == 0) return typeof(object); Type ret = types[0]; for (int i = 1; i < types.Length; ++i) { if (types[i].IsAssignableFrom(ret)) ret = types[i]; else {
I also tested:
Type t = GetCommonBaseClass(typeof(OleDbCommand), typeof(OdbcCommand), typeof(SqlCommand));
And got typeof(DbCommand) . And with the help of:
Type t = GetCommonBaseClass(typeof(OleDbCommand), typeof(OdbcCommand), typeof(SqlCommand), typeof(Component));
And got typeof(Compoment) . And with the help of:
Type t = GetCommonBaseClass(typeof(OleDbCommand), typeof(OdbcCommand), typeof(SqlCommand), typeof(Component), typeof(Component).BaseType);
And got typeof(MarshalByRefObject) . And with
Type t = GetCommonBaseClass(typeof(OleDbCommand), typeof(OdbcCommand), typeof(SqlCommand), typeof(Component), typeof(Component).BaseType, typeof(int));
And got typeof(object) .
Don Griffin Mar 31 '09 at 16:26 2009-03-31 16:26
source share