Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extending c# syntax with roslyn

Tags:

c#

roslyn

I'm trying to implement a "return if"/"return value if" without an else case, since I only want to return or return a value if a condition is valid.

I know, there is the if (condition) return; or if (condition) return value; but I want to have the code a bit cleaner AND it would be nice to have that syntax since it is more readable.

I heard with roslyn this is possible and read the question with its answeres here: Is there a way to implement custom language features in C#? but I have no clue how to implement it.

So the code would be something like this:

public class Testclass
{
    public static void Main(String[] args)
    {
        Testclass t = new Testclass();
        t.TestMyClassWithVoid();
        bool success = t.TestMyClassWithInt(3) == 3;
        bool fail = t.TestMyClassWithInt(2) == 2;
    }

    public void TestMyClassWithVoid()
    {
        int value = GetValueFromSomeWhere();
        return if value == 3;

        DoSomeOtherStuffSinceValueIsNotThree();
    }

    public int TestMyClassWithInt(int value)
    {
        return value if value == 3;

        DoSomeOtherStuffSinceValueIsNotThree();
        return -1;
    }
}

any idea how I could solve this? I started trying with the Roslyn.Compilers.CSharp-namespace but I have no clue how to start implementing.

like image 204
Matthias Burger Avatar asked Aug 05 '16 09:08

Matthias Burger


People also ask

How to extend C drive in Windows 10?

select partition m : m is the number of C drive. extend size= x: x is the size in MB you want to add to the system partition. Note: Same as the Disk Management, the DiskPart can only extend C drive when there is an adjacent unallocated space on its right side.

How to extend system C drive with unallocated space?

Option 1. Extend System C drive with unallocated space 1. Right-click on the System C: drive and select "Resize/Move". 2. Drag the system partition end into the unallocated space so to add it to the C: drive. And click "OK". 3. Click "Execute Operation" and "Apply" to execute the operations and extend the C drive. Option 2.

How to extend C drive after shrinking D?

As you see, the 20GB Unallocated is non adjacent to C drive after shrinking D, of course Extend Volume is greyed out. The only way to enable Extend Volume for C drive is by deleting the contiguous partition (D). Don't do this if you installed program to this partition. Why not extend C drive without causing damage?

How to extend C drive with EASEUS partition Master?

Click the Adjust button to extend it. Step 2. Click "OK" to extend C drive automatically. By clicking "OK", EaseUS Partition Master will automatically allocate space to your C drive to solve the low space issue. Extra Option: You can also click "Manual Adjustment" to extend the C drive manually.


2 Answers

If you can generate the correct IL for this, this should be work. Because this is not a new CLR capability you don't need do anything with dot net core, just Roslyn.

To do that you have two options,

One is to leave Roslyn as is and rewrite your new syntax to the correspond C# syntax and then compile as regular.

Second option, because Roslyn is an open source, is to add the ability to compile you new syntax to your own Roslyn and compile your code with it.

like image 154
Dudi Keleti Avatar answered Sep 19 '22 16:09

Dudi Keleti


Here is an alternative suggestion that may work for someone who is looking for extension points into roslyn. It can even be provided as a nuget package! Create an analyser and a code fix provider for your syntax.

Essentially what you would get is a red squiggly line under any syntax of your choosing, with a auto-fix hint which would let you transform it into the regular C# equivalent.

It could also be used for validating the usage of your library, checking that the right attributes are in the right locations, detect common code smells, etc.

Read up more on code analysers at this post: https://msdn.microsoft.com/en-us/magazine/dn879356.aspx

A quick summary of that link (but by no means a replacement):

  • Install VS SDK, Roslyn SDK, Compiler Platform SDK
  • Create new project: Analyzer with Code Fix (NuGet + VSIX) template.
  • There will be 3 samples on how to create an analyser / code fix provider
  • Set the VSIX project as the startup project and run
like image 31
caesay Avatar answered Sep 21 '22 16:09

caesay