Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeConverter.FindBestMatch do not match js number back to enum #203

Closed
changhaohao opened this issue Jul 1, 2015 · 0 comments · Fixed by #843
Closed

TypeConverter.FindBestMatch do not match js number back to enum #203

changhaohao opened this issue Jul 1, 2015 · 0 comments · Fixed by #843

Comments

@changhaohao
Copy link

c#==================
enum E{a,b }
class A{
public void testFunc(string e){}
public void testFunc(E e){}
}
js=================
var a=new A();
a.testFunc(E.a); // can not find testFunc(E e){}

E.a will convert to double in js
when a.testFunc(E.a); TypeConverter.FindBestMatch can not find a perfect match
but double is close to c# Enum than c# string
=========modified TypeConverter.FindBestMatch seem to solve the issue========
public static IEnumerable FindBestMatch(Engine engine, MethodBase[] methods, JsValue[] arguments)
{
methods = methods
.Where(m => m.GetParameters().Count() == arguments.Length)
.ToArray();

        if (methods.Length == 1 && !methods[0].GetParameters().Any())
        {
            yield return methods[0];
            yield break;
        }
        MethodBase enumMethod = null;
        var objectArguments = arguments.Select(x => x.ToObject()).ToArray();
        foreach (var method in methods)
        {
            var perfectMatch = true;
            var parameters = method.GetParameters();
            for (var i = 0; i < arguments.Length; i++)
            {
                var arg = objectArguments[i];
                var paramType = parameters[i].ParameterType;

                if (arg == null)
                {
                    if (!TypeIsNullable(paramType))
                    {
                        perfectMatch = false;
                        break;
                    }
                }
                else if (arg.GetType() != paramType)
                {
                    perfectMatch = false;
                    if(arg.GetType()==typeof(double)&&paramType.IsEnum)
                    {
                        enumMethod=method;
                    }
                    else 
                    {   enumMethod=null;
                        break;
                    }
                }
            }

            if (perfectMatch)
            {
                yield return method;
                yield break;
            }
        }

        if (enumMethod != null) 
        {
            yield return enumMethod;
            yield break;
        }

        foreach (var method in methods)
        {
            yield return method;
        }
    }
@changhaohao changhaohao changed the title TypeConverter.FindBestMatch do not math js number back to enum TypeConverter.FindBestMatch do not match js number back to enum Jul 2, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant