首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >EnvDTE删除类属性

EnvDTE删除类属性
EN

Stack Overflow用户
提问于 2015-09-04 18:18:41
回答 1查看 221关注 0票数 0

我有一个非常简单的查询(EnvDTE),“如何从类中删除属性”。

代码语言:javascript
复制
[Authorize]
class SomeController
{ ... }

我试过了-

代码语言:javascript
复制
authorizeAttr.StartPoint.CreateEditPoint().Delete(authorizeAttr.EndPoint);

它可以工作,但会留下空方括号。

代码语言:javascript
复制
[]
class SomeController
{ ... }

我只想删除属性,完整的一行(应该是这样的)。请帮帮忙。

谢谢

EN

回答 1

Stack Overflow用户

发布于 2015-12-28 20:23:26

您将需要类的EnvDTE80.CodeClass2,然后删除循环中的属性,如下所示:

代码语言:javascript
复制
EnvDTE80.CodeClass2 codeClass;
...
while (codeClass.Attributes.Count > 0) {
    ((EnvDTE80.CodeAttribute2)codeClass.Attributes.Item(1)).Delete(); // Attributes array starts from 1 not 0
}

下面是一个完整的示例:

代码语言:javascript
复制
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace WindowsFormsApplication1
{
    [MyCustom("Example 1", 1), MyCustom("Example 2", 2)]
    public class MyForm : MyBaseForm
    {
        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // MyForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.ClientSize = new System.Drawing.Size(617, 450);
            this.Name = "MyForm";
            this.ResumeLayout(false);

        }

        #endregion

        public MyForm()
        {
            InitializeComponent();
        }
    }

    [Designer(typeof(MyBaseFormDesigner), typeof(IRootDesigner))]
    public partial class MyBaseForm : Form
    {
        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // MyBaseForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(391, 337);
            this.ResumeLayout(false);

        }

        #endregion

        public MyBaseForm()
        {
            InitializeComponent();
        }
    }

    [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
    public class MyCustomAttribute : Attribute
    {
        public string Text { get; set; }
        public int IntValue { get; set; }

        public MyCustomAttribute(string text, int intValue)
        {
            this.Text = text;
            this.IntValue = intValue;
        }
    }

    public class MyBaseFormDesigner : DocumentDesigner
    {
        public override void Initialize(IComponent component)
        {
            base.Initialize(component);
            Verbs.Add(new DesignerVerb("Remove Attributes", OnRemoveAttributes));            
        }

        protected virtual void OnRemoveAttributes(object sender, EventArgs args)
        {
            EnvDTE._DTE dte = GetService(typeof(EnvDTE._DTE)) as EnvDTE._DTE;
            EnvDTE80.CodeClass2 codeClass = GetCodeClass(dte.ActiveDocument.ProjectItem.FileCodeModel.CodeElements, "MyForm");
            while (codeClass.Attributes.Count > 0) {
                ((EnvDTE80.CodeAttribute2)codeClass.Attributes.Item(1)).Delete(); // Attributes array starts from 1 not 0
            }
        }

        private static EnvDTE80.CodeClass2 GetCodeClass(EnvDTE.CodeElements codeElements, string name)
        {
            EnvDTE80.CodeClass2 codeClass = null;
            foreach (EnvDTE.CodeElement item in codeElements) {
                if (item.Kind == EnvDTE.vsCMElement.vsCMElementClass) {
                    codeClass = item as EnvDTE80.CodeClass2;
                } else if (item.Kind == EnvDTE.vsCMElement.vsCMElementNamespace) {                    
                    codeClass = GetCodeClass(((EnvDTE.CodeNamespace)item).Members, name);
                }
                if (codeClass != null && codeClass.Name == name) break;
            }
            return codeClass;
        }
    }
}

要测试此示例,请编译它,在设计器模式下打开MyForm,右键单击表单并选择“移除属性”,MyForm中的所有属性都应被移除。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32395626

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档