コード例 #1
0
		public static bool ConvertArguments(MethodInfo methodInfo, object thisObj, object[] args, int argCount, ref object[] outArgs)
		{
			MethodBinder methodBinder;
			if (sMethodInfoCache.TryGetValue(methodInfo, out methodBinder) == false)
			{
				methodBinder = new MethodBinder(methodInfo, false);		// We assume that if we reached by this code path, it is not an extension method
																		// TODO: This assumption might need more care
				sMethodInfoCache.Add(methodInfo, methodBinder);
			}
			return methodBinder.ConvertArguments(thisObj, args, argCount, ref outArgs);
		}
コード例 #2
0
        public static bool ConvertArguments(MethodInfo methodInfo, object thisObj, object[] args, int argCount, ref object[] outArgs)
        {
            MethodBinder methodBinder;

            if (sMethodInfoCache.TryGetValue(methodInfo, out methodBinder) == false)
            {
                methodBinder = new MethodBinder(methodInfo, false);                             // We assume that if we reached by this code path, it is not an extension method
                // TODO: This assumption might need more care
                sMethodInfoCache.Add(methodInfo, methodBinder);
            }
            return(methodBinder.ConvertArguments(thisObj, args, argCount, ref outArgs));
        }
コード例 #3
0
        public static MethodBinder[] BuildMethodList(Type type, string name, BindingFlags flags, int argCount)
        {
            var list         = new List <MethodBinder>();
            var variadicList = new List <MethodBinder>();

            // rename toString to ToString for non-playscript objects
            if (name == "toString" && argCount == 0)
            {
                name = "ToString";
            }

            // get methods from main type
            if (type != null)
            {
                var methods = type.GetMethods(flags);
                foreach (var method in methods)
                {
                    if (method.Name == name && !method.ContainsGenericParameters)
                    {
                        var newInfo = new MethodBinder(method, false);
                        if (newInfo.CheckArgumentCount(argCount))
                        {
                            if (!newInfo.IsVariadic)
                            {
                                list.Add(newInfo);
                            }
                            else
                            {
                                variadicList.Add(newInfo);
                            }
                            sMethodInfoCache[method] = newInfo;
                        }
                    }
                }
            }

            if ((flags & BindingFlags.Static) == 0)
            {
                // get extension methods
                var extType = PlayScript.Dynamic.GetExtensionClassForType(type);
                if (extType != null)
                {
                    var methods = extType.GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
                    foreach (var method in methods)
                    {
                        if (method.Name == name)
                        {
                            // determine if this is a valid extension method for this object type
                            var parameters = method.GetParameters();
                            if (parameters.Length > 0)
                            {
                                var thisType = parameters[0].ParameterType;
                                if (thisType.IsAssignableFrom(type))
                                {
                                    var newInfo = new MethodBinder(method, true);
                                    if (newInfo.CheckArgumentCount(argCount))
                                    {
                                        if (!newInfo.IsVariadic)
                                        {
                                            list.Add(newInfo);
                                        }
                                        else
                                        {
                                            variadicList.Add(newInfo);
                                        }
                                        sMethodInfoCache[method] = newInfo;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            // see which variadic methods we want to keep
            // this is necessary in the case of overloads that have the same arguments but one is variadic and one is not
            // for example:
            //       push(o:Object);
            //       push(o:Object, ...);
            // in this case we select the first method when only one argument is supplied
            foreach (var variadic in variadicList)
            {
                bool keepVariadic = true;

                // look at each non-variadic method
                // if any one matches this variadic method then dont keep it
                foreach (var method in list)
                {
                    if (!method.IsVariadic)
                    {
                        bool sameSignature = true;
                        for (int i = 0; i < argCount; i++)
                        {
                            if (variadic.Parameters[i].ParameterType != method.Parameters[i].ParameterType)
                            {
                                sameSignature = false;
                            }
                        }
                        if (sameSignature)
                        {
                            // dont keep this variadic
                            keepVariadic = false;
                            break;
                        }
                    }
                }

                if (keepVariadic)
                {
                    // add variadic to main list
                    list.Add(variadic);
                }
            }

            // return list
            return(list.ToArray());
        }
コード例 #4
0
		public static MethodBinder[] BuildMethodList(Type type, string name, BindingFlags flags, int argCount)
		{
			var list = new List<MethodBinder>();

			// rename toString to ToString for non-playscript objects
			if (name == "toString" && argCount == 0) {
				name = "ToString";
			}

			// get methods from main type
			if (type != null) {
				var methods = type.GetMethods(flags);
				foreach (var method in methods)	{
					if (method.Name == name) {
						var newInfo = new MethodBinder(method, false);
						if (newInfo.CheckArgumentCount(argCount)) {
							list.Add(newInfo);
						}
					}
				}
			}

			if ((flags & BindingFlags.Static)==0) {
				// get extension methods
				var extType = PlayScript.Dynamic.GetExtensionClassForType(type);
				if (extType != null) {
					var methods = extType.GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
					foreach (var method in methods)	{
						if (method.Name == name) {
							// determine if this is a valid extension method for this object type
							var parameters = method.GetParameters();
							if (parameters.Length > 0){
								var thisType = parameters[0].ParameterType;
								if (thisType.IsAssignableFrom(type)) {
									var newInfo = new MethodBinder(method, true);
									if (newInfo.CheckArgumentCount(argCount)) {
										list.Add(newInfo);
									}
								}
							}
						}
					}
				}
			}

			// return list
			return list.ToArray();
		}
コード例 #5
0
		public static MethodBinder[] BuildMethodList(Type type, string name, BindingFlags flags, int argCount)
		{
			var list = new List<MethodBinder>();
			var variadicList = new List<MethodBinder>();

			// rename toString to ToString for non-playscript objects
			if (name == "toString" && argCount == 0) {
				name = "ToString";
			}

			// get methods from main type
			if (type != null) {
				var methods = type.GetMethods(flags);
				foreach (var method in methods)	{
					if (method.Name == name) {
						var newInfo = new MethodBinder(method, false);
						if (newInfo.CheckArgumentCount(argCount)) {
							if (!newInfo.IsVariadic) {
								list.Add(newInfo);
							} else {
								variadicList.Add(newInfo);
							}
							sMethodInfoCache[method] = newInfo;
						}
					}
				}
			}

			if ((flags & BindingFlags.Static)==0) {
				// get extension methods
				var extType = PlayScript.Dynamic.GetExtensionClassForType(type);
				if (extType != null) {
					var methods = extType.GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
					foreach (var method in methods)	{
						if (method.Name == name) {
							// determine if this is a valid extension method for this object type
							var parameters = method.GetParameters();
							if (parameters.Length > 0){
								var thisType = parameters[0].ParameterType;
								if (thisType.IsAssignableFrom(type)) {
									var newInfo = new MethodBinder(method, true);
									if (newInfo.CheckArgumentCount(argCount)) {
										if (!newInfo.IsVariadic) {
											list.Add(newInfo);
										} else {
											variadicList.Add(newInfo);
										}
										sMethodInfoCache[method] = newInfo;
									}
								}
							}
						}
					}
				}
			}

			// see which variadic methods we want to keep
			// this is necessary in the case of overloads that have the same arguments but one is variadic and one is not
			// for example:
			//       push(o:Object);		
			//       push(o:Object, ...);
			// in this case we select the first method when only one argument is supplied
			foreach (var variadic in variadicList) {
				bool keepVariadic = true;

				// look at each non-variadic method
				// if any one matches this variadic method then dont keep it 
				foreach (var method in list) {
					if (!method.IsVariadic) {
						bool sameSignature = true;
						for (int i=0; i < argCount; i++) {
							if (variadic.Parameters[i].ParameterType != method.Parameters[i].ParameterType) {
								sameSignature = false;
							}
						}
						if (sameSignature) {
							// dont keep this variadic
							keepVariadic = false;
							break;
						}
					}
				}

				if (keepVariadic) {
					// add variadic to main list
					list.Add(variadic);
				}
			}

			// return list
			return list.ToArray();
		}