简体   繁体   English

如何将两个C#批注组合到“快捷方式”批注中?

[英]How can I combine two C# annotations into a “shortcut” annotation?

In Unity, I've found it's a handy pattern to declare inspector variables (set by reflection or magic by Unity) as private and to annotate them with the [SerializeField] attribute. 在Unity中,我发现将检查器变量(通过Unity的反射或魔术设置)声明为私有并使用[SerializeField]属性对其进行注释是一种方便的模式。 This keeps programming interfaces clean while allowing the Inspector to set the fields. 这样可以使编程接口保持干净,同时允许Inspector设置字段。 I also use Resharper, and to let it know that these field are set implicitly I use the attribute [UsedImplicitly(ImplicitUseKindFlags.Assign)] . 我还使用Resharper,让我知道这些字段是隐式设置的,我使用属性[UsedImplicitly(ImplicitUseKindFlags.Assign)]

This leaves me with a lot of variables as a brick wall of annotations where finding the variables takes unnecessary effort: 这给我留下了很多变量,就像注释的砖墙一样,在这些砖墙上查找变量会花费不必要的精力:

[SerializeField, UsedImplicitly(ImplicitUseKindFlags.Assign)]
private Transform headingLabel;
[SerializeField, UsedImplicitly(ImplicitUseKindFlags.Assign)]
private Transform coolDataLabel;
[SerializeField, UsedImplicitly(ImplicitUseKindFlags.Assign)]
private GameObject yetAnotherLabelToMakeThePoint;

How can I combine these annotations into a single annotation [PrivateInspectorVariable] with identical behavior? 如何将这些注释合并为具有相同行为的单个注释[PrivateInspectorVariable]

Via this answer on Stack Overflow: 通过这个关于堆栈溢出的答案

However there is nothing in the standard tools that will take two attribute instances and make one. 但是,标准工具中没有任何东西可以接受两个属性实例并构成一个。

(In theory a post processor that re-wrote the MSIL could do this.) (理论上,重新编写MSIL的后处理器可以做到这一点。)

My suggestion would be to write your attributes on separate lines and use space to make them easier to read: 我的建议是将属性写在单独的行上,并使用空间使它们更易于阅读:

[SerializeField]
[UsedImplicitly(ImplicitUseKindFlags.Assign)]
private Transform headingLabel;

[SerializeField]
[UsedImplicitly(ImplicitUseKindFlags.Assign)]
private Transform coolDataLabel;

[SerializeField]
[UsedImplicitly(ImplicitUseKindFlags.Assign)]
private GameObject yetAnotherLabelToMakeThePoint;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM