You are calling InvokeMember
incorrectly. Notice the last parameter of InvokeMember
, You are not passing anything into getKey
, so null is appropriate there. Also, the binding flags werent allowing you to get the proper method.
return (string)myType.InvokeMember("getKey",
BindingFlags.InvokeMethod|
BindingFlags.Public |
BindingFlags.DeclaredOnly |
BindingFlags.Instance,
null,
myObj,
null);
Your Service method:
namespace NFSe.Classes.Models.Classes.NFSeWeb
{
public class Service
{
public string IdService { get; set; }
public string Name { get; set; }
public string getKey()
{
return IdService + Name;
}
}
}
Your calling method:
static void Main(string[] args)
{
var mystring = getValorDaClasse("NFSe.Classes.Models.Classes.NFSeWeb.Service");
}
public static string getValorDaClasse(object valor)
{
if (valor.ToString().Contains("NFSe.Classes.Models.Classes"))
{
Type myType = Type.GetType(valor.ToString());
object myObj = Activator.CreateInstance(myType);
//Invoking a non-static method (How to invoke a non static method??)
return (string)myType.InvokeMember("getKey", BindingFlags.InvokeMethod|BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance, null, myObj, null);
}
else
return valor.ToString();
}