Click here to Skip to main content
15,867,330 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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.
VB
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.
VB
<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
Updated 23-Dec-11 6:39am
v4
Comments
weirbear 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.

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:

C#
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
//...


—SA
 
Share this answer
 
Comments
Monjurul Habib 23-Dec-11 15:40pm    
5!
Sergey Alexandrovich Kryukov 23-Dec-11 16:26pm    
Thank you, Monjurul.
--SA
Wendelius 23-Dec-11 16:07pm    
Good answer 5'd
Sergey Alexandrovich Kryukov 23-Dec-11 16:26pm    
Thank you, Mika.
--SA
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.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 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
Wendelius 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.
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.
 
Share this answer
 
v4

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900