Ejemplo n.º 1
0
    public MethodInfo ResolveFunction(string name, Type[] parameterTypes)
    {
        MethodInfo bestMethod     = null;
        int        bestSimilarity = int.MaxValue;

        foreach (MethodInfo methodInfo in type.GetMethods())
        {
            if (!methodInfo.IsStatic)
            {
                continue;
            }

            if (methodInfo.Name.Equals(name))
            {
                ParameterInfo[] methodParameters = methodInfo.GetParameters();
                if (parameterTypes.Length != methodParameters.Length)
                {
                    continue;
                }

                int  similarity = 0;
                bool matched    = true;
                for (int i = 0; i < parameterTypes.Length; ++i)
                {
                    if (parameterTypes[i] == null)
                    {
                        continue;
                    }

                    Type methodParameterType = methodParameters[i].ParameterType;
                    if (!parameterTypes[i].Equals(methodParameterType))
                    {
                        matched = false;
                    }
                    similarity += TypeCoercer.GetTypeSimilarity(methodParameterType, parameterTypes[i]);
                }

                if (matched)
                {
                    return(methodInfo);
                }
                else if (similarity < bestSimilarity)
                {
                    bestMethod = methodInfo;
                }
            }
        }

        return(bestMethod);
    }
Ejemplo n.º 2
0
    private OperatorInfo FindOperator(Type type, string operatorName, object operand)
    {
        OperatorInfo bestMethod     = null;
        int          bestSimilarity = int.MaxValue;

        foreach (MethodInfo methodInfo in type.GetMethods())
        {
            if (!methodInfo.IsStatic)
            {
                continue;
            }
            else if (!methodInfo.Name.Equals(operatorName))
            {
                continue;
            }

            ParameterInfo[] methodParameters = methodInfo.GetParameters();
            if (methodParameters.Length != 2)
            {
                continue;
            }

            Type operandType = methodParameters[1].ParameterType;
            if (operand == null || operandType.Equals(operand.GetType()))
            {
                return(new OperatorInfo(methodInfo, operandType));
            }

            int similarity = TypeCoercer.GetTypeSimilarity(operandType, operand.GetType());
            if (similarity < bestSimilarity)
            {
                bestMethod = new OperatorInfo(methodInfo, operandType);
            }
        }

        return(bestMethod);
    }