Skip to content

枚举类型属性

如果属性的类型是枚举,那么在设计器中会以下拉列表的形式提供给用户编辑

csharp
    public class MyPluginServerCommand : Command, ICommandExecutableInServerSideAsync
    {
        public UserType MyProperty1 { get; set; }

        public async Task<ExecuteResult> ExecuteAsync(IServerCommandExecuteContext dataContext)
        {
            return new ExecuteResult();
        }
        public override CommandScope GetCommandScope()
        {
            return CommandScope.ExecutableInServer;
        }
    }
    public enum UserType
    {
        Student,
        Teacher,
        Worker
    }

在设计器中效果如下

1669643529091-c230dc76-26be-45d2-a467-7be9aef5a13b.png

自定义枚举项目显示名称, 可以通过标注 Description 实现

csharp
    public class MyPluginServerCommand : Command, ICommandExecutableInServerSideAsync
    {
        public UserType MyProperty1 { get; set; }

        public async Task<ExecuteResult> ExecuteAsync(IServerCommandExecuteContext dataContext)
        {
            return new ExecuteResult();
        }
        public override CommandScope GetCommandScope()
        {
            return CommandScope.ExecutableInServer;
        }
    }
    public enum UserType
    {
        [Description("学生")]
        Student,
        [Description("教师")]
        Teacher,
        [Description("工人")]
        Worker
    }

效果
1670299006576-d52ba910-10e9-43ac-8afa-39cc4d5ed7dc.png

更新: 2022-12-06 11:56:51
原文: https://www.yuque.com/robert-bh51n/ea8l6c/pdvnoxhbfli6hi0g