You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否为ReSharper创建自定义JetBrains.Annotations注解?

解决自定义JetBrains注解替代[UsedImplicitly]的问题

嘿,我刚好处理过类似的需求,因为UsedImplicitlyAttribute确实是密封类没法直接继承,不过有两个简单的方案能实现你要的效果:

方案一:通过ReSharper配置绑定自定义注解

这个方法是让ReSharper把你的自定义注解当成内置的[UsedImplicitly]来处理,步骤如下:

  1. 先创建你自己的UnityAnimationEventAttribute类,不用继承任何JetBrains注解,就是一个普通的特性:
using System;

[AttributeUsage(AttributeTargets.Method | AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Class, Inherited = false)]
public sealed class UnityAnimationEventAttribute : Attribute
{
}
  1. 打开ReSharper的设置(快捷键:Alt+Enter → 选择Settings,或者通过File菜单进入)
  2. 导航到Code Inspection → Settings → Annotations
  3. 在「Custom Attributes」区域点击「Add」,然后选择你刚才创建的UnityAnimationEventAttribute
  4. 在「Treat as」下拉菜单里选择UsedImplicitly,保存设置即可

这样之后,任何标记了[UnityAnimationEvent]的成员,ReSharper都会自动抑制未使用的警告,和[UsedImplicitly]效果完全一致。

方案二:用[MeansImplicitUse]标记自定义注解

这个方法更便携,不需要团队成员统一配置ReSharper,直接在代码里给自定义注解加上ReSharper的底层标记:

using System;
using JetBrains.Annotations;

[AttributeUsage(AttributeTargets.Method | AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Class, Inherited = false)]
[MeansImplicitUse(ImplicitUseKindFlags.Default, ImplicitUseTargetFlags.Default)]
public sealed class UnityAnimationEventAttribute : Attribute
{
}

[MeansImplicitUse]是ReSharper用来识别“隐含使用”类注解的核心标记,UsedImplicitlyAttribute本身就是靠这个标记实现功能的。给你的自定义注解加上它之后,ReSharper会自动识别并应用和[UsedImplicitly]相同的规则。

两种方案各有优势:方案一适合团队统一规范,方案二更适合跨团队或者需要打包分发的代码库。

内容的提问来源于stack exchange,提问作者Rob8861

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:02:59