Skip to content

Proposal: Type-Safe Attributes with Constrained typeof<T> Parameters in C# #8274

Closed Answered by colejohnson66
ameritusweb asked this question in Language Ideas
Discussion options

You must be logged in to vote

Attributes can be generic as well.

https://sharplab.io/#v2:CYLg1APgAgTAjAWAFDINoEEAumBOBLAIwFdMBTAVQGcBDAc1IAotdCTSAVanezSgOnQAbQQEoAusigBmAASVS1QaWAzYMgLIBPZvmJkdrMgB52APhkgZBvaWQz7MgO4ALUjlIz2FmQEkAYtQAxqQA3MiSsngAdmQ4AGZBHv6JYShIqFrWbCaklJgAwoLUlJSmEkjScgpKKmrsuQVFJd7JwSFAA==

[AttributeUsage(AttributeTargets.All)]
public sealed class MyAttributeAttribute<T> : Attribute
    where T : IFace;

public interface IFace;

[MyAttribute<TestClass>]
public sealed class TestClass : IFace;

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@ameritusweb
Comment options

@ameritusweb
Comment options

@colejohnson66
Comment options

Answer selected by ameritusweb
@ameritusweb
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants