
上QQ阅读APP看书,第一时间看更新
How to do it...
- Open Visual Studio 2017.
- 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.
- Press Ctrl + Shift + B for a quick build to check that everything is fine.
- Now click on the Chapter2.Reflections solution label. Click File | Add | New Project.
- In the Add New Project template dialog box, expand the Visual C# node in the left-hand pane.
- Select .NET Core and then Console App (.NET Core) in the right-hand pane:

- 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:

- Click OK.
- Now, the Solution Explorer (press Ctrl + Alt + L) should look like this:

- Right-click on the Dependencies label in the Chapter2.Reflections.CalculatorLib.
- Select Add Reference.
- In the Reference Manager, click on the Projects label in the right-hand pane.
- Check the Chapter2.Reflections.CalculatorLib project in the left-hand pane.
- Click OK.
- Now, double-click on Program.cs to open the code window.
- In the code window, scroll to the top of the screen.
- Next to the last line of the using directives, add this using directive:
using System.Reflection;
using Chapter2.Reflections.CalculatorLib;
- 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();
- Let's hit F5 and see the output:

- Press any key to exit.