项目之间的自定义属性 [英] Custom Attributes Between Projects

查看:75
本文介绍了项目之间的自定义属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个VS 2005解决方案(VB.NET),其中包含许多项目.在一个项目中,我创建了一个自定义属性类.我可以将属性分配给其他项目中的类的属性.

我的问题是该属性未附加在其他项目中.该属性附加在定义它的项目中.如果我在另一个项目中继承我的自定义属性类,则将其附加.

如何在项目之间使用我的自定义属性类,而不必在每个项目中创建继承的类?

我不想继承自定义属性,但这是迄今为止唯一有效的解决方案.

这是我的自定义属性类.很基础.

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.

推荐答案

有一件事不明白:该属性未附加到项目中.可以将其连接到装配体.当您了解它时,您将不会遇到不同项目"的问题-根本没关系.

您必须隔离两个不同的事物:首先,访问在一个程序集中声明的属性类.这是通过另一个程序集引用一个程序集来完成的.这样,您可以从引用的程序集中访问任何公共类型,而不管属性not是否如此.您只能访问那些类型的公共成员.

另一个完全不同的是应用程序或属性的附件.首先,每个属性类都有一组要应用的目标.这是由属性类的作者通过将称为System.AttributeUsageAttribute的特殊属性应用于属性类来定义的.
请参阅 http://msdn.microsoft.com/en-us/library/system.attributeusageattribute.aspx [ ^ ] .

仅当属性[AttributeUsage(/* ... */)]的应用位置包含属性System.AttributeTargets.Assembly时,您的属性才可应用于程序集.请参阅 http://msdn.microsoft.com/en-us/library/system.attributetargets.aspx [^ ] .

最后,将属性附加到程序集需要特殊语法.也许,这就是您所错过的.

您可以在"AssemblyInfo.cs"中看到有关此语法的示例.例如:

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



—SA


创建一个类库项目,在其中放置属性,并在其他项目中添加对该dll的引用.这样,您可以在项目之间共享示例类.
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.


您是否可以在其自己的程序集中实现自定义属性,然后在需要的地方使用它?

编辑====================

哇-1票.您的确使我们希望继续为您提供帮助.自定义属性是否定义为internal?

顺便说一句,从自定义属性继承是一种不好的做法.它们应该是sealed.

最后,由于您没有向我们展示任何示例代码,因此我们只能在黑暗中进行尝试.
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.


这篇关于项目之间的自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆