Browse Source

2008-05-15 Jb Evain <[email protected]>

	* Extensions.cs: add a few useful extensions such as
	IsGenericInstanceOf and MakeGenericFrom.


svn path=/trunk/mcs/; revision=103283
Jb Evain 17 years ago
parent
commit
db413fc328
1 changed files with 29 additions and 0 deletions
  1. 29 0
      mcs/class/System.Core/System.Linq.Expressions/Extensions.cs

+ 29 - 0
mcs/class/System.Core/System.Linq.Expressions/Extensions.cs

@@ -35,6 +35,14 @@ namespace System.Linq.Expressions {
 
 	static class Extensions {
 
+		public static bool IsGenericInstanceOf (this Type self, Type type)
+		{
+			if (!self.IsGenericType)
+				return false;
+
+			return self.GetGenericTypeDefinition () == type;
+		}
+
 		public static bool IsAssignableTo (this Type self, Type type)
 		{
 			return type.IsAssignableFrom (self) ||
@@ -46,6 +54,27 @@ namespace System.Linq.Expressions {
 			return self.GetGenericArguments () [0];
 		}
 
+		public static Type MakeGenericTypeFrom (this Type self, Type type)
+		{
+			return self.MakeGenericType (type.GetGenericArguments ());
+		}
+
+		public static MethodInfo MakeGenericMethodFrom (this MethodInfo self, MethodInfo method)
+		{
+			return self.MakeGenericMethod (method.GetGenericArguments ());
+		}
+
+		public static Type [] GetParameterTypes (this MethodBase self)
+		{
+			var parameters = self.GetParameters ();
+			var types = new Type [parameters.Length];
+
+			for (int i = 0; i < types.Length; i++)
+				types [i] = parameters [i].ParameterType;
+
+			return types;
+		}
+
 		static bool ArrayTypeIsAssignableTo (Type type, Type candidate)
 		{
 			if (!type.IsArray || !candidate.IsArray)