Roslyn vs Reflection for TypeScript Code Generator

I am developing a TypeScript code generator that will use custom attributes on C # classes to generate TypeScript code definitions and files.

I am considering two options for TypeScript code / source file generation:

  • Reflection on compiled assemblies
  • Roslyn CTP

The tool will use custom attributes for properties and methods to create a TypeScript file. Right now, I do not plan to convert the body of the C # method to JavaScript, but this can be done in the future. Therefore, for this reason, I am serious about Roslin. However, to just create an outline of my TypeScript classes, I think I could use reflection and custom attributes.

I am wondering:

a) Does Roslyn provide functionality that is not possible with Reflection? I understand that I cannot get the bodies of methods with Reflection.

b) Will the Roslyn CTP license provide my distribution tool with an open source license? This is not clear to me after reading the license

+7
source share
7 answers

The Roslyn website does not clearly state that:

The current license is for evaluation and preview purposes only. does not allow redistributing Roslyn binaries. Sharing example projects built on the Roslyn API are allowed, but sample users must have either the Roslyn CTP or the Roslyn NuGet package installed to build and run.

0
source

I just did something in this direction - works great to create your datamodel in Typescript from your C # classes. I built it to create a single AMD module with an interface that simulates the basic data of your models. It also supports Generics and creates a class with Knockout properties, including the toJS () method and the update (data: Interface) method to update your class.

All this is just one T4 template. If someone finds this and is interested: http://spabuilder.wordpress.com/2014/07/31/generating-typescript-from-c/

Also respects the [KeyAttribute] and [Timespan] attributes for data models if you use data annotations.

+2
source

Doesn't the license forbid you to personally distribute binary files? It does not prevent you from adding a dependency on your NuGet package to the Rosynn CTP NuGet package. You personally cannot deliver the bit, but you can force NuGet to automatically insert Roslyn.

Therefore, just do not check the source or binaries of Rosyln in your version control.

+1
source

I have been creating js and I consider Reflection the best tool for this. I basically point my generator to the bin folder of the project from which the metadata comes from. There may be some difficulties in loading all the necessary assemblies and warnings with the versions of assemblies in the bin folder and versions of the same assemblies as the project of your generator. But as soon as you deal with all this, which I did with minimal difficulties, Reflection is much easier to use and more reliable.

With Roslyn, you just parse C #. Roslyn does this very well, but I hesitate to switch to her with Reflection. With reflection, you get more reliable metadata.

Suppose you want the RoutePrefixAttribute attribute prefix property to decorate a controller class. If you are parsing C #, you might have: [RoutePrefix ("stringliteral")] or [RoutePrefix (constantString)]. So you need to worry about whether it is a literal or constant expression, and then find out how to get the value of the constant expression, worry about all the different ways of passing parameters to the atatribute attribute (for example, will it break your code: [RoutePrefix ( Prefix = "literal")] ...

Once you are dealing with real run-time objects with reflection, everything is simply simpler. You have a nice RoutePrefixAttribute object, and you can go routePrefix.Prefix to get, reliably, the prefix value.

This is just one example of how to make things easier with Reflection. This is the difference between collecting metadata from a set of C # objects in a safe way and scrambling data from C # code, albeit with a really nice cleaning tool.

EDIT: After writing this answer, I removed the bullet and switched to Roslyn. It is powerful enough as soon as you get it, and I found one big advantage: you can get a link to the workspace from the visual studio plugin and easily do all kinds of things in the plugin.

+1
source

Update - July 2015

Roslyn is still in CTP, but their GitHub FAQ is much more important:

For sample code or learning objectives, the recommended Roslyn DLL redistribution method is associated with the Roslyn NuGet package: [url: Microsoft.CodeAnalysis | http://www.nuget.org/packages/Microsoft.CodeAnalysis] .

Thus, you still cannot redistribute DLLs in finished products. The project must be open, and a solution will require a link to the NuGet package.



Original answer (November 2012)

I do not believe that you can distribute it open source.

6. COMMON CODE . The software contains code that you are allowed to distribute in programs that you develop if you run below.

6.c Distribution restrictions You may not modify or distribute the source code of any distributed code to any part of it subject to the Exclusion License. An excluded license is one that requires, as terms of use, modification or distribution,

  • The code must be disclosed or distributed in source code form; or element
  • others have the right to change it.

At first it sounds like you can do it if you just include the Roslyn binaries, but the definition of the Distributable Code in particular says: β€œThe software contains the code ...”, and I think this is what we are talking about after .

For your other question, Roslyn is not completely finished and is still in beta. I do not know for sure if he is currently in a state that allows him to cope with your needs. This is something you might just want to spend a couple of hours on. I would not think that it has more functionality than .NET allows. You can see what they recently added in September here and what is not currently implemented here .

0
source

I would not use the current Roslyn CTP - simply because new versions will appear in 2014, and this will surely bring a lot of changes. This way you can get completely outdated code.

(He was recently posted on the blog by a member of the MS team, but I'm afraid I don't have a link right now.)

Edit
There is a good chance that Roslyn will receive a license that also permits commercial use ...

0
source

For my experience of using T4 generations based on reflection, like TypeLite, it is somehow simpler, but it has some drawbacks, for example, when a project depends on the created classes, regenerating them with a change (renaming the class) leads to an uncompiled project, so the launch The template again leads to the release of the blanck file, and it will be difficult for the user to compile everything.

So, having the same need, I started experimenting with Roslyn, and it seems very promising, but I have a lot of doubts about how to use it correctly ...

You can see what I'm doing, and maybe help me here: https://github.com/TrabacchinLuigi/RoslynExporter

0
source

All Articles