D
D
DancingOnWater2014-10-23 12:29:32
C++ / C#
DancingOnWater, 2014-10-23 12:29:32

How to get an overloaded method using reflection, where one of the parameters is ref or out?

Situation. It is necessary to call TryParse using reflection (for example, Double)
But there are two problems, TryParse is an overloaded method. and secondly, I specifically need
TryParse(string, out double)
In theory, this code should work:

var modefier = new ParameterModifier(2);
modefier[0] = false;
            modefier[1] = true;
            MethodInfo mi1 =
 typeof(Double).GetMethod("TryParse", new Type[]{typeof(string), typeof(Double)}, new ParameterModifier[]{modefier});

However, null is returned. CHANTD?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
aush, 2014-10-23
@DancingOnWater

MethodInfo mi1 = typeof(Double).GetMethod("TryParse",
              BindingFlags.Static | BindingFlags.Public,
              null,
              new Type[] { typeof(string), typeof(Double).MakeByRefType() },
              null);

S
smet4ik, 2014-10-23
@smet4ik

MethodInfo TryParseDouble = typeof(double).GetMethod("TryParse",
                BindingFlags.Public | BindingFlags.Static,
                null,
                CallingConventions.Any,
                new[] { typeof(string), typeof(double).MakeByRefType() },
                null);

            object[] parameters = {"1818,1818", 0.0 };
            double rezult;
            if ((bool)TryParseDouble.Invoke(null, parameters))
            {
                rezult = (double)parameters[1];
            }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question