1
我發現了一個ControlDesigner的例子,它向我展示瞭如何使用IEventBindingService
添加控件並創建一個事件處理程序,然後使用CodeTypeDeclaration
在該事件處理程序中添加一些代碼。但是,當我嘗試訪問基本表單的自定義屬性CodeTypeDeclaration
時,返回了一個空集合。下面的例子表明,CodeTypeDeclaration
不返回基本形式的任何自定義屬性:在設計時訪問表單的自定義屬性
using System;
using System.CodeDom;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;
namespace WindowsFormsApplication1
{
[MyCustom("new sample text")]
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.ResumeLayout(false);
}
#endregion
public MyForm()
{
InitializeComponent();
}
}
[MyCustom("sample text")]
[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 MyCustomAttribute(string text)
{
this.Text = text;
}
}
public class MyBaseFormDesigner : DocumentDesigner
{
public override void Initialize(IComponent component)
{
base.Initialize(component);
Verbs.Add(new DesignerVerb("Show CodeTypeDeclaration", OnShowCodeTypeDeclaration));
}
private static string GetCode(CodeTypeDeclaration codeType)
{
var code = new System.Text.StringBuilder();
using (var provider = new Microsoft.CSharp.CSharpCodeProvider()) {
using (var writer = new System.IO.StringWriter(code)) {
provider.GenerateCodeFromType(codeType, writer, new System.CodeDom.Compiler.CodeGeneratorOptions());
}
}
return code.ToString();
}
protected virtual void OnShowCodeTypeDeclaration(object sender, EventArgs args)
{
var codeType = GetService(typeof(CodeTypeDeclaration)) as CodeTypeDeclaration;
if (MessageBox.Show("Add MyCustomAttribute?", "", MessageBoxButtons.YesNo) == DialogResult.Yes) {
codeType.CustomAttributes.Add(new CodeAttributeDeclaration(new CodeTypeReference(typeof(MyCustomAttribute)), new CodeAttributeArgument(new CodePrimitiveExpression("sample text from designer"))));
}
MessageBox.Show(GetCode(codeType));
}
}
}
我嘗試使用自定義CodeDomSerializer
我的形式,但這種做法我只能在InitializeComponent
方法訪問代碼。有沒有其他方法可以訪問我的表單的自定義屬性?
我想要這個的原因是我可以在設計器中創建一個動作,在窗體上添加/更改我的自定義屬性的參數。