How to print Hello World on console using reflection in C#?
using System;
namespace ConsoleProject
{
class Program
{
static void Main(string[] args)
{
//get the method by method name and with string argument types
//Here GetMethod() will return the info of WriteLine(string value)
//and invoke() will call WriteLine method by passing the string "Hello World"
//this line equivalent to Console.WriteLine("Hello World");
typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }).Invoke(default, new object[] { "Hello World" });
Console.ReadLine();
}
}
}
Comments
Post a Comment