Java Annotations 和 C# Attributes 有什么异同?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/553857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 07:55:42  来源:igfitidea点击:

What are the similarities and differences between Java Annotations and C# Attributes?

c#javaattributesannotationscustom-attributes

提问by Jared

I have a Java library I'm considering porting to C#. The Java library makes extensive use of annotations (at both build time and run time.)

我有一个正在考虑移植到 C# 的 Java 库。Java 库广泛使用了注解(在构建时和运行时)。

I've never used C# attributes, but understand that they are the rough equivalent of Java annotations.

我从未使用过 C# 属性,但我知道它们大致相当于 Java 注释。

If I proceed with the port using attributes to replace annotations, what do I need to know? What's going to be the same? Different? What's going to bite me?

如果我继续使用属性替换注释的端口,我需要知道什么?什么会是一样的?不同的?什么会咬我?

回答by Jon Skeet

One important aspect of Java annotations which I haven't looked at in detail yet: you can write code which uses annotations within javac (as of Java 6, I believe) as a form of metaprogramming.

我还没有详细研究过 Java 注释的一个重要方面:您可以编写使用 javac 中的注释的代码(我相信从 Java 6 开始)作为元编程的一种形式

PostSharpallows something similar, admittedly.

诚然,PostSharp允许类似的事情。

(That's far from the only difference, but it's all I have time for right now.)

(这远不是唯一的区别,但这是我现在唯一的时间。)

回答by Scott Stanchfield

Following up to what Jon said...

按照乔恩所说的话……

Both Java 5 and Java 6 allow metaprogramming. Java 5 uses a separate apt tool; Java 6 integrates it into the compiler. (Though the Java 5 apt is still available in Java 6)

Java 5 和 Java 6 都允许元编程。Java 5 使用了一个单独的 apt 工具;Java 6 将其集成到编译器中。(虽然 Java 5 apt 在 Java 6 中仍然可用)

Unfortunately each uses a different API.

不幸的是,每个都使用不同的 API。

I'm currently using the Java 5 API for my Bean annotations

我目前正在为我的 Bean 注释使用 Java 5 API

See http://code.google.com/p/javadude/wiki/Annotationsfor an example (NOTE: I'm currently making some major updates, some of which change the API.)

有关示例,请参阅http://code.google.com/p/javadude/wiki/Annotations(注意:我目前正在进行一些重大更新,其中一些更改了 API。)

Very cool stuff... -- Scott

非常酷的东西...--斯科特

回答by serg10

One interesting difference is that C# allows module and assembly level attributes.These are applied to your module or assembly and are available via reflection in the normal way. Java does not provide reflection on a jar file, so jar level annotations are not possible.

一个有趣的区别是 C# 允许模块和程序集级别的属性。这些应用于您的模块或程序集,并且可以通过正常方式通过反射获得。Java 不提供对 jar 文件的反射,因此 jar 级别的注释是不可能的。

In fact this is quite common as Visual Studio a file called AssemblyInfo.csat the root project level which contains, for example:

事实上,这很常见,因为 Visual Studio 是一个AssemblyInfo.cs在根项目级别调用的文件,其中包含,例如:

[assembly: AssemblyVersionAttribute("1.2.3.4")]
[assembly: AssemblyTitleAttribute("My project name blah blah")]
...

There is no equivalent jar level annotation in Java (although since jdk5, there is a little used package level annotation mechanism - thanks to mmeyersfor pointing that one out)

Java 中没有等效的 jar 级别注释(尽管自 jdk5 以来,有一些使用的包级别注释机制 - 感谢mmeyers指出这一点)

回答by serg10

Control over when your metadata is made accessible is different between the two languages.

两种语言对何时可以访问元数据的控制不同。

Java provides the java.lang.annotation.Retentionannotation and java.lang.annotation.RetentionPolicyenumto control when annotation metadata is accessible. The choices vary from Runtime(most common - annotation metadata retained in class files), to Source(metadata discarded by compiler). You tag your custom annotation interface with this - for example:

Java 提供了java.lang.annotation.Retention注释和java.lang.annotation.RetentionPolicy枚举来控制何时可以访问注释元数据。选择从Runtime(最常见 - 保留在类文件中的注释元数据)到Source(编译器丢弃的元数据)不等。你用这个标记你的自定义注释接口 - 例如:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.CLASS)
public @interface TraceLogging {
  // etc
}

would allow you to reflect on your custom TraceLoggingannotation at runtime.

将允许您TraceLogging在运行时反映您的自定义注释。

C# uses the ConditionalAttributeattributethat is driven from compile time symbols. So the analogous example in C# is:

C# 使用由编译时符号驱动ConditionalAttribute属性。因此,C# 中的类似示例是:

[Conditional("TRACE")]
public class TraceLoggingAttribute : Attribute
{
  // etc
}

which would cause the compiler to spit out the metadata for your custom TraceLoggingattribute only if the TRACEsymbol was defined.

这会导致编译器TraceLoggingTRACE在定义了符号时才为您的自定义属性吐出元数据。

NB. attribute metadata is available at runtime by default in C# - this is only needed if you want to change that.

注意。默认情况下,C# 中的属性元数据在运行时可用 - 仅当您想更改它时才需要。