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.)
我有一个Java库,我正在考虑移植到C#。 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?
如果我使用属性来替换注释继续使用端口,我需要知道什么?什么会是一样的?不同?什么会咬我?
4 个解决方案
#1
24
Control over when your metadata is made accessible is different between the two languages.
控制何时可以访问元数据在两种语言之间是不同的。
Java provides the java.lang.annotation.Retention annotation and java.lang.annotation.RetentionPolicy enum to 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枚举,以控制何时可以访问批注元数据。选项从运行时(最常见 - 类文件中保留的注释元数据)到源(编译器丢弃的元数据)不等。您可以使用此标记自定义注释界面 - 例如:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.CLASS)
public @interface TraceLogging {
// etc
}
would allow you to reflect on your custom TraceLogging
annotation at runtime.
允许您在运行时反映自定义TraceLogging注释。
C# uses the ConditionalAttribute attribute that 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 TraceLogging
attribute only if the TRACE
symbol was defined.
只有在定义了TRACE符号时,才会导致编译器为自定义TraceLogging属性吐出元数据。
NB. attribute metadata is available at runtime by default in C# - this is only needed if you want to change that.
NB。默认情况下,在C#中,属性元数据在运行时可用 - 只有在您想要更改它时才需要。
#2
13
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),这是一种元编程的形式。
PostSharp allows something similar, admittedly.
不可否认,PostSharp允许类似的东西。
(That's far from the only difference, but it's all I have time for right now.)
(这远远不是唯一的区别,但现在我只有时间。)
#3
10
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.cs
at 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 mmeyers for pointing that one out)
在Java中没有等效的jar级别注释(尽管从jdk5开始,有一些使用的包级别注释机制 - 感谢mmeyers指出那个)
#4
2
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
我目前正在使用Java 5 API进行Bean注释
See http://code.google.com/p/javadude/wiki/Annotations for 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
很酷的东西...... - 斯科特
#1
24
Control over when your metadata is made accessible is different between the two languages.
控制何时可以访问元数据在两种语言之间是不同的。
Java provides the java.lang.annotation.Retention annotation and java.lang.annotation.RetentionPolicy enum to 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枚举,以控制何时可以访问批注元数据。选项从运行时(最常见 - 类文件中保留的注释元数据)到源(编译器丢弃的元数据)不等。您可以使用此标记自定义注释界面 - 例如:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.CLASS)
public @interface TraceLogging {
// etc
}
would allow you to reflect on your custom TraceLogging
annotation at runtime.
允许您在运行时反映自定义TraceLogging注释。
C# uses the ConditionalAttribute attribute that 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 TraceLogging
attribute only if the TRACE
symbol was defined.
只有在定义了TRACE符号时,才会导致编译器为自定义TraceLogging属性吐出元数据。
NB. attribute metadata is available at runtime by default in C# - this is only needed if you want to change that.
NB。默认情况下,在C#中,属性元数据在运行时可用 - 只有在您想要更改它时才需要。
#2
13
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),这是一种元编程的形式。
PostSharp allows something similar, admittedly.
不可否认,PostSharp允许类似的东西。
(That's far from the only difference, but it's all I have time for right now.)
(这远远不是唯一的区别,但现在我只有时间。)
#3
10
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.cs
at 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 mmeyers for pointing that one out)
在Java中没有等效的jar级别注释(尽管从jdk5开始,有一些使用的包级别注释机制 - 感谢mmeyers指出那个)
#4
2
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
我目前正在使用Java 5 API进行Bean注释
See http://code.google.com/p/javadude/wiki/Annotations for 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
很酷的东西...... - 斯科特