C# implement late binding via type in runtime,dynamically call method via type.GetMethod() and parameters type array
static void RuntimeGetTypeLateBinding() { object s = "Hello"; PropertyInfo pi = s.GetType().GetProperty("Length"); Console.WriteLine((int)pi.GetValue(s, null)); }
Dynamically call method GetMethod() via reflection and late binding in runtime
static void MethodParametersDemo() { Type type = typeof(string); Type[] paraTypes = { typeof(int) }; MethodInfo mi=type.GetMethod ("Substring",paraTypes); Object[] arguments = { 2 }; object returnValue = mi.Invoke("Meditation", arguments); Console.WriteLine(returnValue); }
static void TypeRetrieveInvokeGenericMethods() { var result = from m in typeof(Enumerable).GetMethods() where m.Name == "Where" && m.IsGenericMethod let parameters = m.GetParameters() where parameters.Length == 2 let genArg = m.GetGenericArguments().First() let enumerableOfT = typeof(IEnumerable<>).MakeGenericType(genArg) let funcOfTBool = typeof(Func<,>).MakeGenericType(genArg, typeof(bool)) where parameters[0].ParameterType == enumerableOfT && parameters[1].ParameterType == funcOfTBool select m; foreach(var mi in result) { Console.WriteLine($"{mi.Name},{mi.ReturnParameter}"); } }