ExtraConstraints help to add generic constraint supported by .Net and not by C#

ExtraConstraints use Fody’s way to add generic contraint:

Your Code

public class Sample
    public void MethodWithDelegateConstraint<[DelegateConstraint] T> () {...}

    public void MethodWithTypeDelegateConstraint<[DelegateConstraint(typeof(Func<int>))] T> () {...}

    public void MethodWithEnumConstraint<[EnumConstraint] T>() {...}

    public void MethodWithTypeEnumConstraint<[EnumConstraint(typeof(ConsoleColor))] T>() {...}

What gets compiled

public class Sample
    public void MethodWithDelegateConstraint<T>() where T: Delegate {...}

    public void MethodWithTypeDelegateConstraint<T>() where T: Func<int> {...}

    public void MethodWithEnumConstraint<T>() where T: struct, Enum {...}

    public void MethodWithTypeEnumConstraint<T>() where T: struct, ConsoleColor {...}

Source: Fody/ExtraConstraints: Facilitates adding constraints for Enum and Delegate to types and methods.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s