.NET Standard 2.0 Cookbook
上QQ阅读APP看书,第一时间看更新

How to do it...

  1. Open Visual Studio 2017.
  2. Now open the solution from the previous recipe. Click File | Open | Open Project/Solution, or press Ctrl + Shift + O, and select the Chapter2.Reflections solution. 
  3. Press Ctrl + Shift + B for a quick build to check that everything is fine. 
  4. Now click on the Chapter2.Reflections solution label. Click File | Add | New Project.
  5. In the Add New Project template dialog box, expand the Visual C# node in the left-hand pane. 
  1. Select .NET Core and then Console App (.NET Core) in the right-hand pane:
  1. Now, in the Name: text box, type Chapter2.Reflections.ReflectCore as the name of the project. The rest of the fields should be left as defaults:
  1. Click OK.
  1. Now, the Solution Explorer (press Ctrl + Alt + L) should look like this: 
  1. Right-click on the Dependencies label in the Chapter2.Reflections.CalculatorLib.
  2. Select Add Reference.
  3. In the Reference Manager, click on the Projects label in the right-hand pane. 
  4. Check the Chapter2.Reflections.CalculatorLib project in the left-hand pane.
  5. Click OK.
  6. Now, double-click on Program.cs to open the code window. 
  7. In the code window, scroll to the top of the screen. 
  8. Next to the last line of the using directives, add this using directive: 
      using System.Reflection;
using Chapter2.Reflections.CalculatorLib;
  1. Again, scroll down until you reach the Main() method and write the following code in between the curly brackets of the Main() method: 
      MemberInfo info = typeof(Calculator);
Console.WriteLine($"Assembly Name: {info.Name}");
Console.WriteLine($"Module Name: {info.Module.Name}");
Console.WriteLine();

var calculator = new Calculator();
var typeObject = calculator.GetType();
var methods = typeObject.GetRuntimeMethods();

foreach (var method in methods)
{
Console.WriteLine($"Method name : {method.Name},
---> Return type : {method.ReturnType}");

}

Console.ReadLine();
  1. Let's hit F5 and see the output: 
  1. Press any key to exit.