Created
July 26, 2013 01:30
-
-
Save Layoric/6085319 to your computer and use it in GitHub Desktop.
Method invoke by reflection - Fixed version of http://stackoverflow.com/questions/2202381/reflection-how-to-invoke-method-with-parameters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Assembly assembly = Assembly.LoadFile("...Assembly1.dll"); | |
Type type = assembly.GetType("TestAssembly.Main"); | |
if (type != null) | |
{ | |
MethodInfo methodInfo = type.GetMethod(methodName); | |
if (methodInfo != null) | |
{ | |
object result = null; | |
ParameterInfo[] parameters = methodInfo.GetParameters(); | |
object classInstance = Activator.CreateInstance(type, null); | |
if (parameters.Length == 0) | |
{ | |
result = methodInfo.Invoke(classInstance, null); | |
} | |
else | |
{ | |
object[] parametersArray = new object[] { "Hello" }; | |
result = methodInfo.Invoke(classInstance, parametersArray); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment