There is, and it’s already answered in English here
https://stackoverflow.com/questions/8499593/c-sharp-how-to-check-if-namespace-class-or-method-exists-in-c
More precise answer in English and translated:
You can resolve a Type from a string by using the Type.Gettype(String) >method. For example:
To solve a class from a String you can use the Type.Gettype(String)
Type myType = Type.GetType("MyNamespace.MyClass");
You can then use this Type instance to check if a method exists on the >type by Calling the Getmethod(String) method. For example:
From the class, it is possible to validate the existence of a function through the Getmethod(String) function. For example:
MethodInfo myMethod = myType.GetMethod("MyMethod");
Both Gettype and Getmethod Return null if no type or method was found for the Given name, so you can check if your type/method exist by checking if your method call returned null or not.
Both the Gettype and Getmethod function will return null case for the given String, there is no class or function respectively.
Finally, you can instantiate your type using >Activator.Createinstance(Type) For example:
Finally, from the data obtained, it is possible to instantiate an object of the class from the function Activator.Createinstance(Type)
object instance = Activator.CreateInstance(myType);
Regarding the second part of the question, to get the parameters, there is the Getparameters() function that belongs to class Methodinfo.:
ParameterInfo[] pars = myMethod.GetParameters();
foreach (ParameterInfo p in pars) {
Console.WriteLine(p.ParameterType);
}
Check where? What for?
– Maniero
I’ll change the question to make it more specific
– Artur Trapp
Why do this? All exist, is ready there, this use makes no sense. C# is not a dynamic language that the function may or may not exist. You’d need to show a case where that’s useful, in which case the
VerificaExistenciaFuncoes()
does nothing that is no longer known to the programmer.– Maniero
So now you need to give us more information than you actually want so we can see what to do...
– Maniero