1
public sealed class test<T> : IEnumerable<T> where T : new()
{
     public sealed class Ignore : Attribute { }
}    

public class test_2
{
     [ ???? ]
     string helllo;
}

Is there a way to access the Attribute from outside the test<T> class? I can't seem to find one.

Chris Rau
  • 13
  • 3
  • I believe your class needs to be called IgnoreAttribute to work properly. – pquest Dec 03 '14 at 13:44
  • 1
    `[test.Ignore]`. You must specify a type for `T`. This is because the type parameter `T` is available to nested types, even though your attributes doesn't use it. – vcsjones Dec 03 '14 at 13:44
  • 2
    This become very awkward to use - why have a non-generic attribute inside a generic class? Putting Attributes inside classes isn't really good practice anyway – Rhumborl Dec 03 '14 at 13:46
  • But I cannot specify a type for T, intellisense isnt proposing some, nor does the compliler accept these @vcsjones – Chris Rau Dec 03 '14 at 14:00
  • Any class nested inside a generic class actually represents a different class for each different generic parameter. So `test.Ignore` is a different type than `test.Ignore`. However, as bbeda answered below, C# does not support generic attributes so you cannot embed an attribute class inside a generic one and use it as an attribute. – Grax32 Dec 03 '14 at 15:06

1 Answers1

1

C# does not support Generic Attributes. I know your attribute is not generic but I suppose you want it inside that generic class to use T in attribute level, which makes the Attribute generic (You'll get different Ignore type for each T)

Unfortunately you can't achieve this in C#. Why does C# forbid generic attribute types?

Updated from comments: You can have the Ignore attribute in the scope of the namespace. The you can do reflection on test<T> and look for properties with Ignore attribute.

public sealed class Ignore : Attribute { }

public sealed class test<T> : IEnumerable<T> where T : new()
{
    [Ignore]
    public test_2 SomeProperty { get; set; }
}


public class test_2
{
    [Ignore]
    public string TestData { get; set; }
}
Community
  • 1
  • 1
Bogdan Beda
  • 758
  • 5
  • 12
  • `public sealed class test : IEnumerable where T : new() { public sealed class Ignore : Attribute { } } public class test_2 { [test.Ignore] string helllo; }` Actually not, the T is totally uninteresting for the attribute, they won't even ever be near each other in code :D The Attribute is just to identify vars to be ignored by the reflection of `test` – Chris Rau Dec 03 '14 at 14:14
  • Yes, but you could do that if you wanted to. C# does not support that to avoid such complexities. If they are not related why do you want to put the attribute inside that class anyway? – Bogdan Beda Dec 03 '14 at 14:25
  • the attribute is related to `test`, that's why I want it in there, but it's needed by other classes, so that the user can specify variables to be ignored by `test` – Chris Rau Dec 03 '14 at 14:34
  • I what sense is it related to test? Conceptually, the Ignore attribute has no relation to your class. It may only if it'd do not do some fancy things using the class (which isn't the case now). They should not be related, as their purpose is totally different. From a design point of you, Ignore should not be related in any way with your class. – Bogdan Beda Dec 03 '14 at 14:39
  • But ignore is needed by the `test` class, because the user needs it to define vars to be ignored, if it is not in the `test` class, how would a user use it then? – Chris Rau Dec 03 '14 at 14:50
  • Thanks for trying, but it's needed vice versa, the [ignore] is to be on test_2 TestData. And how would I make sure, that the user, who uses my `test` class has this attribute, or even know of it? – Chris Rau Dec 03 '14 at 14:57
  • What stops you adding the Ignore attribute to the test2.TestData property? You can add Ignore attribute to any property as long as the assembly and the namespace are referenced.How does a user know...? Well...how does a user know to put [Serializable] attribute on serializable classes? She gets a runtime exception! and reads the documentation:) – Bogdan Beda Dec 03 '14 at 15:06