How .NET CORE dynamically calls generic solutions

巴扎黑
Release: 2017-09-01 14:36:50
Original
2094 people have browsed it

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;
    }
  }
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template