using System; using System.Collections.Generic; namespace Program { public class AxControlEventArgs : EventArgs where T : WindowlessControl { public T Control { get; private set; } public AxControlEventArgs(T control) { Control = control; } } public class AxControlEventArgs : AxControlEventArgs { public AxControlEventArgs(WindowlessControl c) : base(c) { } } public class AlignPadLayoutPanel : AlignPadLayoutPanel { } public class AlignPadLayoutPanel : WindowlessControl where T : WindowlessControl { protected override void OnControlAdded(AxControlEventArgs e) { e.Control.Resize += Content_Resize; //OK if removed base.OnControlAdded(e); } private void Content_Resize(object sender, EventArgs e) { } } public class GroupBox : AlignPadLayoutPanel { } //NOT OK //public class GroupBox : AlignPadLayoutPanel {} //OK public class Program { public static readonly AttachedProperty EDITING_TEXT = new AttachedProperty(null); static void Main(string[] args) { Console.WriteLine("Program 6"); var gr = new GroupBox(); //gr.InsertControl(0, new WindowlessControl()); //OK /* need this block(A) this to crash */ var item = new WindowlessControl(); new AlignPadLayoutPanel().InsertControl(0, item); //OK if removed item.SetAttachedProperty(EDITING_TEXT, "label.Text"); /*end block(A)*/ gr.InsertControl(0, new WindowlessControl()); //NOT OK var wc = new WindowlessControl(); wc.SetAttachedProperty(EDITING_TEXT, "label.Text"); var str = wc.GetAttachedProperty(EDITING_TEXT); Console.WriteLine("DONE"); } } public class WindowlessControl { internal readonly Dictionary _AttachedProperties = new Dictionary(); public void SetAttachedProperty(AttachedProperty prop, T val) { Console.WriteLine("SetAttachedProperty 1"); _AttachedProperties[prop] = val; Console.WriteLine("SetAttachedProperty 2"); } public T GetAttachedProperty(AttachedProperty prop) { Console.WriteLine("GET AttachedProperty"); return (T)(_AttachedProperties[prop] ?? prop.DefaultValue); } public event EventHandler Resize; public void InsertControl(int index, WindowlessControl control) { OnControlAdded(new AxControlEventArgs(control)); } protected virtual void OnControlAdded(AxControlEventArgs e) { } } public struct AttachedProperty { public T DefaultValue { get; private set; } public AttachedProperty(T defaultValue) : this() { DefaultValue = defaultValue; } } }