Home >Backend Development >C#.Net Tutorial >How .NET CORE dynamically calls generic solutions

How .NET CORE dynamically calls generic solutions

巴扎黑
巴扎黑Original
2017-09-01 14:36:502111browse

This article mainly introduces .NET CORE to dynamically call generic methods in detail. It has certain reference value. Interested friends can refer to it.

The examples in this article share .NET with everyone. CORE dynamically calls generic methods for your reference. The specific content is as follows


using System;
using System.Reflection;

namespace DynamicCall
{
  class Program
  {
    static void Main(string[] args)
    {
      Console.WriteLine("Hello World!");
      Program p = new Program();
      var ti = p.GetType().GetTypeInfo();
      var mtd = ti.GetMethod("Get");

      Console.WriteLine(mtd?.ToString() ?? "no get method.");

      var genMethod = mtd.MakeGenericMethod(typeof(int));

      var obj = genMethod.Invoke(p, new object[] { });

      Console.WriteLine(obj?.ToString() ?? "no get result.");

      Console.ReadLine();
    }

    public string Get<T>()
    {
      return typeof(T).FullName;
    }
  }
}

The above is the detailed content of How .NET CORE dynamically calls generic solutions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn