Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET Attributes
I have a VS 2005 solution (VB.NET) that contains a number of projects. In one project I have created a custom attribute class. I can assign the attribute to properties of classes in the other projects.
 
My problem is that the attributes are not being attached in the other projects. The attribute is attached in the project where it is defined. If I inherit my custom attribute class in another project it is then attached.
 
How can my custom attribute class be used between projects without having to create an inherited class in each project?
 
I don't want to inherit from my custom attribute but it is the only solution that has worked so far.
 
This is my custom attribute class. Very basic.
Public Class OrderAttribute
    Inherits Attribute
 
    Private _Index As Integer
 
    Public Sub New(ByVal index As Integer)
        _Index = index
    End Sub
 
    Public ReadOnly Property Index() As Integer
        Get
            Return _Index
        End Get
    End Property
End Class
 
I can add the attribute to properties in other projects but like I mentioned, they are not attached when the code is compiled.
 
This is how I attach the attribute to a property of a class object in another projects.
<Order(0)> _
Public ReadOnly Property Key() As String
    Get
        Return _Key
    End Get
End Property
 
I believe I am coding everything correctly. I don't know what I am missing.
Posted 23-Dec-11 6:41am
Edited 23-Dec-11 7:39am
v4
Comments
weirbear at 23-Dec-11 12:13pm
   
The custom attribute is defined in a project that creates a DLL. Other projects in the solution make reference to this DLL. I can add my custom attribute in the code of the other projects but when compiled the attribute is only attached to properties of objects that are defined in the same project as the custom attribute.
 
Currently the only way I've had success with using the same custom attribute between the projects is to create another class in each that inherits from the custom attribute. I am looking for a way of doing this without having to create the extra inherited classes.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

One thing are do not understand it: the attribute is not attached to the project. It can be attached to the assembly. When you understand it, you won't have a problem with "different project" — is simply does not matter.
 
You have to isolate two different things: first, accessing of an attribute class declared in one assembly. This is done through referencing of one assembly by another assembly. This way, you can access any public types from the referenced assembly, not matter attribute of not. You can only access public members of those type.
 
Another, completely different thing is the application, or attachment of the attribute. First of all, each attribute class has a set of targets to be applied to. This is defined by the author of the attribute class by applying a special attribute called System.AttributeUsageAttribute to an attribute class.
 
See http://msdn.microsoft.com/en-us/library/system.attributeusageattribute.aspx[^].
 
Your attribute can only apply to assembly if the attribute System.AttributeTargets.Assembly is included at the place of application of the attribute [AttributeUsage(/* ... */)]. See http://msdn.microsoft.com/en-us/library/system.attributetargets.aspx[^].
 
Finally, attachment of the attribute to your assembly needs special syntax. Maybe, this is what you missed.
 
You can see the examples on this syntax in your "AssemblyInfo.cs". For example:
 
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
//...
 
—SA
  Permalink  
Comments
Monjurul Habib at 23-Dec-11 15:40pm
   
5!
SAKryukov at 23-Dec-11 16:26pm
   
Thank you, Monjurul.
--SA
Mika Wendelius at 23-Dec-11 16:07pm
   
Good answer 5'd
SAKryukov at 23-Dec-11 16:26pm
   
Thank you, Mika.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Create a Class library project, place your attribute in it and add references to that dll in your other projects. This way you can share for example classes between projects.
  Permalink  
Comments
SAKryukov at 23-Dec-11 15:37pm
   
That is basically correct but that is not it, unfortunately. OP does not understand what is the application of the attribute, how it is done and probably cannot see the difference between that and using assembly, needs clarification. (I did not vote this time.)
 
Please see my answer.
--SA
Mika Wendelius at 23-Dec-11 16:08pm
   
Seems that way. Seems that the question has changed quite a bit. For curiosity have a look at the first version of the question.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Can't you implement the custom attribute in its own assembly, and then use it wherever you need it?
 
EDIT ======================
 
Wow - a 1-vote. You really make us want to keep helping you. Is the custom attribute defined as internal?
 
BTW, it's considered bad practice to inherit from a custom attribute. They SHOULD be sealed.
 
Lastly, we can only take stabs in the dark because you haven't shown us any example code.
  Permalink  
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 23 Dec 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100