版本6.0有一个新功能nameof,但我无法理解它的目的,因为它只需要变量名称并在编译时将其更改为字符串.
我认为它在使用<T>时可能有一些用途,但是当我尝试nameof(T)它时只打印一个T而不是使用的类型.
有意的吗?
在Java中是否可以从实际字段中获取字符串中的字段名称?喜欢:
public class mod {
@ItemID
public static ItemLinkTool linkTool;
public void xxx{
String fieldsName = *getFieldsName(linkTool)*;
}
}
Run Code Online (Sandbox Code Playgroud)
PS:我不是在寻找字段的类/类名或从字符串中获取字段.
编辑:当我看着它时,我可能不需要一个方法来获取字段的名称,Field实例(来自字段的"代码名称")就足够了.[例如Field myField = getField(linkTool)]
在Java本身可能没有我想要的东西.我将看看ASM库,但最后我可能最终使用字符串作为字段的标识符:/
编辑2:我的英语不是很好(但即使用我的母语,我也有解释这个问题),所以我再添加一个例子.希望现在会更清楚:
public class mod2 {
@ItemID
public static ItemLinkTool linkTool;
@ItemID
public static ItemLinkTool linkTool2;
@ItemID
public static ItemPipeWrench pipeWrench;
public void constructItems() {
// most trivial way
linkTool = new ItemLinkTool(getId("linkTool"));
linkTool2 = new ItemLinkTool(getId("linkTool2"));
pipeWrench = new ItemPipeWrench(getId("pipeWrench"));
// or when constructItem would directly write into field just
constructItem("linkTool"); …Run Code Online (Sandbox Code Playgroud)