Skip to content

Durian is a collection of Roslyn-based analyzers and source generators that extend the default capabilities of C#.

License

Notifications You must be signed in to change notification settings

piotrstenke/Durian

Folders and files

NameName
Last commit message
Last commit date

Latest commit

0a0f71f · Jan 30, 2022

History

88 Commits
Jun 13, 2021
Jan 27, 2022
Aug 2, 2021
Jan 30, 2022
Jan 30, 2022
Jan 30, 2022
Jan 30, 2022
Jan 27, 2022
Apr 16, 2021
Apr 16, 2021
Aug 1, 2021
Jan 30, 2022
May 17, 2021
Jan 30, 2022
Jan 30, 2022

Repository files navigation

Durian logo

Durian is a collection of Roslyn-based analyzers, source generators and utility libraries that bring many extensions to C#, with heavy emphasis on features that can be found in other existing languages. It's main goal is to make C# easier and more pleasant to use through reducing necessary boilerplate code, while at the same time providing additional layers of flexibility.

Current state

Durian is at an early stage of its evolution - many core features are still missing, being either in early development or planning phase. As for now, only one fully-fledged module is complete - DefaultParam, with InterfaceTargets and FriendClass awaiting in experimental stage for release in the closest future.

Features

To see more about a specific feature, click on its name.

DefaultParam allows to specify a default type for a generic parameter.

using Durian;

public class Test<[DefaultParam(typeof(string))]T>
{
    public T Value { get; }

    public Test(T value)
    {
        Value = value;
    }
}

public class Program
{
    static void Main()
    {
        // Test<T> can be used without type parameters - 'T' defaults to 'string'.
        Test test1 = new Test("");
        
        // Type parameter can be stated explicitly.
        Test<string> test2 = new Test<string>("");
    }
}

InterfaceTargets, similar to how System.AttributeUsageAttribute works, allows to specify what kinds of members an interface can be implemented by.

using Durian;

[InterfaceTargets(InterfaceTargets.Class)]
public interface ITest
{
}

// Success!
// ITest can be implemented, because ClassTest is a class.
public class ClassTest : ITest
{
}

// Error!
// ITest cannot be implemented, because StructTest is a struct, and ITest is valid only for classes.
public struct StructTest : ITest
{
}

FriendClass allows to limit access to 'internal' members by specifying a fixed list of friend types.

using Durian;

[FriendClass(typeof(A))]
public class Test
{
    internal static string Key { get; }
}

public class A
{
    public string GetKey()
    {
        // Success!
        // Type 'A' is a friend of 'Test', so it can safely access internal members.
        return Test.Key;
    }
}

public class B
{
    public string GetKey()
    {
        // Error!
        // Type 'B' is not a friend of 'Test', so it cannot access internal members.
        return Test.Key;
    }
}

Experimental

Experimental modules include packages that are almost ready to be released, but still need some more polishing.

Currently, there are no modules in the experimental stage.

(Written by Piotr Stenke)

About

Durian is a collection of Roslyn-based analyzers and source generators that extend the default capabilities of C#.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages