Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save thedom85/94b3215269f0d19893f7 to your computer and use it in GitHub Desktop.
Save thedom85/94b3215269f0d19893f7 to your computer and use it in GitHub Desktop.
CSharp_Reflection_GetByName_DynamicCalledMethods
void Main()
{
Console.WriteLine ("Reflection \n");
//Create SampleClass objec
Console.WriteLine ("\n");
Console.Write("1)Create SampleClass object => new SampleClass(): \n");
var myObj = new SampleClass();
//Get the Type information
Console.WriteLine ("\n");
Console.Write("2)Get the Type information => myObj.GetType(): ");
Type myTypeObj = myObj.GetType();
Console.WriteLine ("\n");
Console.Write(string.Format("Name:{0} \n",myObj.GetType().Name));
Console.Write(string.Format("FullName:{0} \n",myObj.GetType().FullName));
//Get Method Information
Console.WriteLine ("\n");
Console.WriteLine ("3) Get Method Information => myTypeObj.GetMethod(Sum2):");
var methodInfo = myTypeObj.GetMethod("Sum2");
Console.WriteLine (string.Format("myMethodInfo.Name:{0} \n",methodInfo.Name));
//Get Method Information
Console.WriteLine ("\n");
object[] mParam = new object[] {5, 10};
Console.Write(string.Format("4) myMethodInfo.Invokee(myObj, mParam):{0}\n" ,methodInfo.Invoke(myObj, mParam)) );
}
public class SampleClass
{
public virtual int Sum2(int numb1,int numb2)
{
int result = numb1 + numb2;
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment