我有一个非常简单的查询(EnvDTE),“如何从类中删除属性”。
[Authorize]
class SomeController
{ ... }
我试过了 -
authorizeAttr.StartPoint.CreateEditPoint().Delete(authorizeAttr.EndPoint);
它有效,但留空方括号。
[]
class SomeController
{ ... }
我只想删除属性,完整的行(应该是这样)。请帮忙。
由于
答案 0 :(得分:0)
您将需要您的类的EnvDTE80.CodeClass2,然后在循环中删除属性,如下所示:
EnvDTE80.CodeClass2 codeClass;
...
while (codeClass.Attributes.Count > 0) {
((EnvDTE80.CodeAttribute2)codeClass.Attributes.Item(1)).Delete(); // Attributes array starts from 1 not 0
}
以下是一个完整的例子:
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中的所有属性。