ebook img

.NET Development Using the Compiler API PDF

175 Pages·2016·6.69 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview .NET Development Using the Compiler API

.neT Development Using the Compiler api — Jason Bock .NET Development Using the Compiler API Jason Bock .NET Development Using the Compiler API Jason Bock Shakopee Minnesota, USA ISBN-13 (pbk): 978-1-4842-2110-5 ISBN-13 (electronic): 978-1-4842-2111-2 DOI 10.1007/978-1-4842-2111-2 Library of Congress Control Number: 2016945755 Copyright © 2016 by Jason Bock This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Exempted from this legal reservation are brief excerpts in connection with reviews or scholarly analysis or material supplied specifically for the purpose of being entered and executed on a computer system, for exclusive use by the purchaser of the work. Duplication of this publication or parts thereof is permitted only under the provisions of the Copyright Law of the Publisher’s location, in its current version, and permission for use must always be obtained from Springer. Permissions for use may be obtained through RightsLink at the Copyright Clearance Center. Violations are liable to prosecution under the respective Copyright Law. Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein. Managing Director: Welmoed Spahr Lead Editor: James DeWolf Technical Reviewer: Fabio Claudio Ferracchiati Editorial Board: Steve Anglin, Pramila Balan, Aaron Black, Louise Corrigan, Jonathan Gennick, Robert Hutchinson, Celestin Suresh John, James Markham, Natalie Pao, Susan McDermott, Matthew Moodie, Ben Renow-Clarke, Gwenan Spearing Coordinating Editor: Melissa Maldonado Copy Editor: Laura Lawrie Compositor: SPi Global Indexer: SPi Global Artist: SPi Global Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail o rders-ny@springer-sbm. com , or visit w ww.springeronline.com . Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation. For information on translations, please e-mail [email protected] , or visit w ww.apress.com . Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at w ww.apress.com/bulk-sales . Any source code or other supplementary materials referenced by the author in this text are available to readers at w ww.apress.com/9781484221105 . For detailed information about how to locate your book’s source code, go to www.apress.com/source-code/ . Readers can also access source code at SpringerLink in the Supplementary Material section for each chapter. Printed on acid-free paper Contents at a Glance About the Author ..............................................................................ix About the Technical Reviewer ..........................................................xi Acknowledgments ..........................................................................xiii Introduction .....................................................................................xv ■ Chapter 1: An Overview of the Compiler API ..................................1 ■ Chapter 2: Writing Diagnostics .....................................................33 ■ Chapter 3: Creating Refactorings and Handling Workspaces .......69 ■ Chapter 4: Using the Scripting API .............................................107 ■ Chapter 5: The Future of the Compiler API .................................139 Index ..............................................................................................155 iii Contents About the Author ..............................................................................ix About the Technical Reviewer ..........................................................xi Acknowledgments ..........................................................................xiii Introduction .....................................................................................xv ■ Chapter 1: An Overview of the Compiler API ..................................1 From Closed to Open ...............................................................................1 What Do Compilers Do? ............................................................................................2 Compilers as a Closed Box .......................................................................................4 Compilers as an Open Box ........................................................................................6 Compiling Code .......................................................................................6 Referencing Assemblies ...........................................................................................6 Building Code ...........................................................................................................8 Creating Code Using Trees ....................................................................10 Visualizing Trees .....................................................................................................10 Building Trees .........................................................................................................17 Navigating and Editing Trees .................................................................20 Finding Content from a Node ..................................................................................21 Finding Content Using Walkers ...............................................................................22 Semantic Models ....................................................................................................23 Editing Trees ...........................................................................................................25 v ■ CONTENTS Annotations and Formatters ..................................................................29 Using Annotations ...................................................................................................29 Using Formatters ....................................................................................................30 Conclusion .............................................................................................32 ■ Chapter 2: Writing Diagnostics .....................................................33 The Need to Diagnose Compilation .......................................................33 Designing the Diagnostic ......................................................................35 Understanding the Problem ....................................................................................35 Using the Syntax Visualizer ....................................................................................36 Creating a Diagnostic ............................................................................37 Using the Template .................................................................................................37 Building the Diagnostic ...........................................................................................39 Providing Code Fixes .............................................................................43 Designing the Fix ....................................................................................................44 Implementing the Fix ..............................................................................................45 Using Syntax Trees .................................................................................................46 Parsing Statements ................................................................................................50 Executing the Diagnostic and Code Fix ..................................................................52 Debugging Diagnostics .........................................................................54 Unit Testing .............................................................................................................54 VSIX Installation ......................................................................................................60 Visual Studio Logging .............................................................................................61 Deploying and Installing Diagnostics ....................................................66 VSIX Packaging .......................................................................................................66 NuGet Packaging ....................................................................................................67 Conclusion .............................................................................................68 vi ■ CONTENTS ■ Chapter 3: Creating Refactorings and Handling Workspaces .......69 Consistency in Structure .......................................................................69 Developing a Refactoring ......................................................................73 Understanding the Problem ....................................................................................73 Creating a Refactoring Solution ..............................................................................75 Building the Refactoring .........................................................................................76 Executing the Refactoring ......................................................................................82 Debugging Refactorings ........................................................................86 Unit Testing .............................................................................................................86 VSIX Installation ......................................................................................................91 Interacting with a Workspace ................................................................91 What Is a Workspace? ............................................................................................91 Updating Solutions and Projects .............................................................................93 Conclusion ...........................................................................................106 ■ Chapter 4: Using the Scripting API .............................................107 What Is a Scripting Language? ...........................................................107 Orchestrating an Environment ..............................................................................107 Dynamic Capabilities ............................................................................................108 Using the C# REPL ...............................................................................109 Loading Code in Script..........................................................................................113 Making C# Interactive .........................................................................114 Referencing the Scripting NuGet Package ...........................................................115 Evaluating Scripts .................................................................................................115 Analyzing Scripts ..................................................................................................119 State Management in Scripts ...............................................................................122 vii ■ CONTENTS Concerns with the Scripting API ..........................................................125 Scripts, Performance, and Memory Usage ...........................................................126 Scripts and Security .............................................................................................130 Conclusion ...........................................................................................138 ■ Chapter 5: The Future of the Compiler API .................................139 Current Usage......................................................................................139 Generating Mocks .................................................................................................139 Building Code with Code.......................................................................................143 Other Compiler API-Based Tools and Frameworks ...............................................147 Looking into C#’s Future ......................................................................147 A Quick Story About Property Change Notifi cations .............................................148 Reusing Common Implementations ......................................................................150 Conclusion ...........................................................................................153 Index ..............................................................................................155 viii About the Author Jason Bock i s a Practice Lead for Magenic ( http://www.magenic.com ) and a Microsoft MVP (C#). He has 20 years of experience working on a number of business applications using a diverse set of frameworks and languages such as C#, .NET, and JavaScript. He is the author of M etaprogramming in .NET, Applied .NET Attributes , and C IL Programming: Under the Hood of .NET . He has written numerous articles on software development issues and has presented at a number of conferences and user groups. He is a leader of the Twin Cities Code Camp ( http://www.twincitiescodecamp.com ). Jason holds a Master’s degree in electrical engineering from Marquette University. Visit his website at h ttp://www.jasonbock.net . ix

Description:
This is the first book to describe the recent significant changes to the Roslyn compiler and demonstrate how .NET developers can use the new Roslyn APIs to create compelling applications. As an open source compiler, Roslyn now makes its code available for anyone to use. In this book, you’ll get a
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.