Cómo lanzar implícitamente en un método reflejado
Tengo una claseThing
eso es implícitamente moldeable desde unstring
. Cuando llamo a un método con unaThing
parámetro directamente el elenco destring
aThing
se hace correctamente.
Sin embargo, si uso la reflexión para llamar al mismo método, se produce la excepción.
System.ArgumentException : Object of type 'System.String' cannot be
converted to type 'Things.Program+Thing'.
Tal vez haya una buena razón para esto, pero no puedo entenderlo. ¿Alguien tiene una idea de cómo hacer que esto funcione utilizando la reflexión?
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, no hay discusiones sobre por qué la conversión o la reflexión implícitas es mala.