Como converter implicitamente em uma chamada de método refletida
Eu tenho uma aulaThing
que é implicitamente convertível de umstring
. Quando eu chamo um método com umThing
parâmetro diretamente o elenco destring
paraThing
é feito corretamente.
No entanto, se eu usar reflexão para chamar o mesmo método, lança a exceção
System.ArgumentException : Object of type 'System.String' cannot be
converted to type 'Things.Program+Thing'.
Talvez haja uma boa razão para isso, mas não consigo entender. Alguém tem uma ideia de como fazer isso funcionar usando a reflexão?
namespace Things
{
class Program
{
public class Thing
{
public string Some;
public static implicit operator Thing(string s)
{
return new Thing {Some = s};
}
}
public void showThing(Thing t)
{
Console.WriteLine("Some = " + t.Some);
}
public void Main()
{
showThing("foo");
MethodInfo showThingReflected = GetType().GetMethod("showThing");
showThingReflected.Invoke(this, new dynamic[] {"foo"});
}
}
}
Meta:Por favor, não há discussões porque o casting implícito ou a reflexão é ruim.