code_junkie

Easiest way to get a common base class from a collection of types

Nov 12th, 2011
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.90 KB | None | 0 0
  1. static void Main(string[] args)
  2. {
  3. Console.WriteLine("Common Types: " + GetCommonBaseClass(new Type[] {typeof(OleDbCommand), typeof(OdbcCommand), typeof(SqlCommand)}).ToString());
  4. }
  5.  
  6. static Type GetCommonBaseClass(Type[] types)
  7. {
  8. if (types.Length == 0)
  9. return (typeof(object));
  10. else if (types.Length == 1)
  11. return (types[0]);
  12.  
  13. // Copy the parameter so we can substitute base class types in the array without messing up the caller
  14. Type[] temp = new Type[types.Length];
  15.  
  16. for (int i = 0; i < types.Length; i++)
  17. {
  18. temp[i] = types[i];
  19. }
  20.  
  21. bool checkPass = false;
  22.  
  23. Type tested = null;
  24.  
  25. while (!checkPass)
  26. {
  27. tested = temp[0];
  28.  
  29. checkPass = true;
  30.  
  31. for (int i = 1; i < temp.Length; i++)
  32. {
  33. if (tested.Equals(temp[i]))
  34. continue;
  35. else
  36. {
  37. // If the tested common basetype (current) is the indexed type's base type
  38. // then we can continue with the test by making the indexed type to be its base type
  39. if (tested.Equals(temp[i].BaseType))
  40. {
  41. temp[i] = temp[i].BaseType;
  42. continue;
  43. }
  44. // If the tested type is the indexed type's base type, then we need to change all indexed types
  45. // before the current type (which are all identical) to be that base type and restart this loop
  46. else if (tested.BaseType.Equals(temp[i]))
  47. {
  48. for (int j = 0; j <= i - 1; j++)
  49. {
  50. temp[j] = temp[j].BaseType;
  51. }
  52.  
  53. checkPass = false;
  54. break;
  55. }
  56. // The indexed type and the tested type are not related
  57. // So make everything from index 0 up to and including the current indexed type to be their base type
  58. // because the common base type must be further back
  59. else
  60. {
  61. for (int j = 0; j <= i; j++)
  62. {
  63. temp[j] = temp[j].BaseType;
  64. }
  65.  
  66. checkPass = false;
  67. break;
  68. }
  69. }
  70. }
  71.  
  72. // If execution has reached here and checkPass is true, we have found our common base type,
  73. // if checkPass is false, the process starts over with the modified types
  74. }
  75.  
  76. // There's always at least object
  77. return tested;
  78. }
  79.  
  80. public static String[] GetCommonPropertiesByName(Object[] objs)
  81. {
  82. List<Type> typeList = new List<Type>(Type.GetTypeArray(objs));
  83. List<String> propertyList = new List<String>();
  84. List<String> individualPropertyList = new List<String>();
  85.  
  86. foreach (Type type in typeList)
  87. {
  88. foreach (PropertyInfo property in type.GetProperties())
  89. {
  90. propertyList.Add(property.Name);
  91. }
  92. }
  93.  
  94. propertyList = propertyList.Distinct().ToList();
  95.  
  96. foreach (Type type in typeList)
  97. {
  98. individualPropertyList.Clear();
  99.  
  100. foreach (PropertyInfo property in type.GetProperties())
  101. {
  102. individualPropertyList.Add(property.Name);
  103. }
  104.  
  105. propertyList = propertyList.Intersect(individualPropertyList).ToList();
  106. }
  107.  
  108. return propertyList.ToArray();
  109. }
  110.  
  111. PropertyInfo p = t.GetType().GetProperty("some Property String Name");
  112. p.GetValue(t, null);
  113. p.SetValue(t, someNewValue, null);
  114.  
  115. public int Compare(T x, T y)
  116. {
  117. PropertyInfo[] props = x.GetType().GetProperties();
  118.  
  119. foreach(PropertyInfo info in props)
  120. {
  121. if(info.name == y.GetType().Name)
  122. ....
  123. }
  124.  
  125. ...
  126.  
  127. class TypeHandler
  128. {
  129. public static List<string> GetCommonProperties(Type[] types)
  130. {
  131. Dictionary<string, int> propertyCounts = new Dictionary<string, int>();
  132.  
  133. foreach (Type type in types)
  134. {
  135. foreach (PropertyInfo info in type.GetProperties())
  136. {
  137. string name = info.Name;
  138. if (!propertyCounts.ContainsKey(name)) propertyCounts.Add(name, 0);
  139. propertyCounts[name]++;
  140. }
  141. }
  142.  
  143. List<string> propertyNames = new List<string>();
  144.  
  145. foreach (string name in propertyCounts.Keys)
  146. {
  147. if (propertyCounts[name] == types.Length) propertyNames.Add(name);
  148. }
  149.  
  150. return propertyNames;
  151. }
  152. }
  153.  
  154. return (from t in types
  155. from p in t.GetProperties()
  156. group p by p.Name into pg
  157. where pg.Count() == types.Length
  158. select pg.Key).ToList();
  159.  
  160. internal class BaseFinder
  161. {
  162. public static Type FindBase(params Type[] types)
  163. {
  164. if (types == null)
  165. return null;
  166.  
  167. if (types.Length == 0)
  168. return null;
  169.  
  170. Dictionary<Type, IList<Type>> baseTypeMap = new Dictionary<Type,IList<Type>>();
  171.  
  172. // get all the base types and note the one with the longest base tree
  173. int maxBaseCount = 0;
  174. Type typeWithLongestBaseTree = null;
  175. foreach (Type type in types)
  176. {
  177. IList<Type> baseTypes = GetBaseTree(type);
  178. if (baseTypes.Count > maxBaseCount)
  179. {
  180. typeWithLongestBaseTree = type;
  181. maxBaseCount = baseTypes.Count;
  182. }
  183. baseTypeMap.Add(type, baseTypes);
  184. }
  185.  
  186. // walk down the tree until we get to a common base type
  187. IList<Type> longestBaseTree = baseTypeMap[typeWithLongestBaseTree];
  188. for (int baseIndex = 0; baseIndex < longestBaseTree.Count;baseIndex++)
  189. {
  190. int commonBaseCount = 0;
  191. foreach (Type type in types)
  192. {
  193. IList<Type> baseTypes = baseTypeMap[type];
  194. if (!baseTypes.Contains(longestBaseTree[baseIndex]))
  195. break;
  196. commonBaseCount++;
  197. }
  198. if (commonBaseCount == types.Length)
  199. return longestBaseTree[baseIndex];
  200. }
  201. return null;
  202. }
  203.  
  204. private static IList<Type> GetBaseTree(Type type)
  205. {
  206. List<Type> result = new List<Type>();
  207. Type baseType = type.BaseType;
  208. do
  209. {
  210. result.Add(baseType);
  211. baseType = baseType.BaseType;
  212. } while (baseType != typeof(object));
  213. return result;
  214. }
  215. }
  216.  
  217. public static Type GetCommonBaseClass (params Type[] types)
  218. {
  219. if (types.Length == 0)
  220. return typeof(object);
  221.  
  222. Type ret = types[0];
  223.  
  224. for (int i = 1; i < types.Length; ++i)
  225. {
  226. if (types[i].IsAssignableFrom(ret))
  227. ret = types[i];
  228. else
  229. {
  230. // This will always terminate when ret == typeof(object)
  231. while (!ret.IsAssignableFrom(types[i]))
  232. ret = ret.BaseType;
  233. }
  234. }
  235.  
  236. return ret;
  237. }
  238.  
  239. Type t = GetCommonBaseClass(typeof(OleDbCommand),
  240. typeof(OdbcCommand),
  241. typeof(SqlCommand));
  242.  
  243. Type t = GetCommonBaseClass(typeof(OleDbCommand),
  244. typeof(OdbcCommand),
  245. typeof(SqlCommand),
  246. typeof(Component));
  247.  
  248. Type t = GetCommonBaseClass(typeof(OleDbCommand),
  249. typeof(OdbcCommand),
  250. typeof(SqlCommand),
  251. typeof(Component),
  252. typeof(Component).BaseType);
  253.  
  254. Type t = GetCommonBaseClass(typeof(OleDbCommand),
  255. typeof(OdbcCommand),
  256. typeof(SqlCommand),
  257. typeof(Component),
  258. typeof(Component).BaseType,
  259. typeof(int));
Add Comment
Please, Sign In to add comment