Call Dapper generic method

CSharp · Console

/Program.cs

using System;
using System.Reflection;
					
	namespace Dapper
	{
		class DbConnection
		{
			public void Insert<T>(T obj)
			{
				Console.WriteLine("Inserting an object with type {0}", typeof(T).FullName);
			}
		}
	}

	namespace MyProgram
	{
		class DbConnector
		{
			protected readonly Dapper.DbConnection _connection = new Dapper.DbConnection();

			public void InsertDynamicObject(object obj)
			{
				typeof(Dapper.DbConnection)
					.GetMethod("Insert")
					.MakeGenericMethod(new [] { obj.GetType() })
					.Invoke(_connection, new[] { obj });
			}
		}

		public class Program
		{
			public static void Main()
			{
				object someObject = "Test";

				var connector = new DbConnector();
				connector.InsertDynamicObject(someObject);
			}
		}
	}
;