DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Data
  4. FastSharp – Building A Fast C# Prototyping Tool Using Roslyn And AvalonEdit

FastSharp – Building A Fast C# Prototyping Tool Using Roslyn And AvalonEdit

Toni Petrina user avatar by
Toni Petrina
·
Sep. 16, 12 · Interview
Like (0)
Save
Tweet
Share
7.51K Views

Join the DZone community and get the full member experience.

Join For Free

FastSharp in action

I’ve had this idea about building a fast code prototyping tool for a while now. You can use LINQPad to test LINQ queries against lots of data sources, but it cannot provide fast code prototyping. The obvious solution was to go and build a custom tool. Open source to the rescue.

For such tool I needed two parts: visual editor for displaying source code and an interpreter to actually compile and execute that code. I wanted to use WPF for building UI and there is an excellent control that offers syntax highlighting – AvalonEdit. As for the code compiling and execution, I use Roslyn – a great project from Microsoft.

To install Roslyn, make sure that you already have installed Visual Studio SDK SP1. If you don’t have it, you can grab it here. You can install the latest version of Roslyn from here.

You can install AvalonEdit quickly via Nuget by executing the following command: Install-Package AvalonEdit. Now you can add it to your XAML window using the following fragment:

// namespace
xmlns:avalonEdit="http://icsharpcode.net/sharpdevelop/avalonedit"

// somewhere else
<avalonEdit:TextEditor Grid.Row="1"
                        FontFamily="Consolas"
                        SyntaxHighlighting="C#"
                        FontSize="14pt"
                        Name="editor"
                        Margin="3" ShowLineNumbers="True" WordWrap="True" KeyDown="editor_KeyDown">
    <avalonEdit:TextEditor.Options>
        <avalonEdit:TextEditorOptions ShowEndOfLine="True"
                                        ShowSpaces="True"
                                        ShowTabs="True" />
    </avalonEdit:TextEditor.Options>
</avalonEdit:TextEditor>

And now the fun part – executing code. First add the necessary references to Roslyn.Compilers and Roslyn.Compilers.CSharp. The quick and dirty way is to create an instance of ScriptEngine and call the Execute method. Here is the code fragment:

var engine = new ScriptEngine();
var session = Session.Create();
engine.Execute(editor.SelectedText, session);

That serves as a basic foundation for the project. Now comes the hard part, defining requirements and building features. You can grab this project over at github, although at this stage it isn’t much.

Open source Visual editor Syntax highlighting Fragment (logic) Grab (software) Execution (computing) Data (computing) Testing GitHub

Published at DZone with permission of Toni Petrina, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Using the PostgreSQL Pager With MariaDB Xpand
  • Top Authentication Trends to Watch Out for in 2023
  • Playwright vs. Cypress: The King Is Dead, Long Live the King?
  • The Data Leakage Nightmare in AI

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: