Click here to Skip to main content
13,049,233 members (72,423 online)
Rate this:
Please Sign up or sign in to 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.
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
            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
        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 5:41am
Updated 23-Dec-11 6:39am
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.
Rate this: bad
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.


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[^].

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("")]

Monjurul Habib 23-Dec-11 15:40pm
SAKryukov 23-Dec-11 16:26pm
Thank you, Monjurul.
Mika Wendelius 23-Dec-11 16:07pm
Good answer 5'd
SAKryukov 23-Dec-11 16:26pm
Thank you, Mika.
Rate this: bad
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.
SAKryukov 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.
Mika 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.
Rate this: bad
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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 23 Dec 2011
Copyright © CodeProject, 1999-2017
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