這確實是一個非常好的問題。正如你所指出的,它是按照你如何註冊事件處理程序的順序排列的。如果你在運行時通過反射操作並改變處理程序的順序,它可以按預期工作。我準備了一個scenerio正如你上面所說的。
這裏我定義的屬性
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class CustomAttribute : Attribute
{
private int _value;
public int Value
{
get { return _value; }
set { _value = value; }
}
private string _eventName;
public string EventName
{
get { return _eventName; }
set { _eventName = value; }
}
public CustomAttribute()
{
}
}
安迪創建自定義文件夾從文本框擴展
public class CustomBox : TextBox
{
public CustomBox()
{
this.PreviewTextInput += CustomBox_TextChanged;
this.PreviewTextInput += CustomBox_PreviewTextInput;
}
protected override void OnInitialized(EventArgs e)
{
base.OnInitialized(e);
foreach (var item in typeof(CustomBox).GetRuntimeMethods().ToList())
{
var a = item.GetCustomAttributes();
// unsubscribe
foreach (var i in a)
{
if (i.GetType() == typeof(CustomAttribute))
{
if (((CustomAttribute)i).Value > 0)
{
RemoveEvent(((CustomAttribute)i).EventName, item.Name);
}
}
}
}
// subscribe according to your order
var methods = typeof(CustomBox).GetRuntimeMethods()
.Where(m => m.GetCustomAttributes(typeof(CustomAttribute), false).Length > 0)
.ToList();
foreach (var item in methods.OrderBy(m => ((CustomAttribute)m.GetCustomAttribute(typeof(CustomAttribute))).Value))
{
AddEvent(((CustomAttribute)item.GetCustomAttribute(typeof(CustomAttribute))).EventName, item.Name);
}
}
private void RemoveEvent(string eventName, string methodName)
{
EventInfo ev = this.GetType().GetEvent(eventName);
Type tDelegate = ev.EventHandlerType;
MethodInfo miHandler = typeof(CustomBox).GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance);
Delegate d = Delegate.CreateDelegate(tDelegate, this, miHandler);
ev.RemoveEventHandler(this, d);
}
private void AddEvent(string eventName,string methodName)
{
EventInfo ev = this.GetType().GetEvent(eventName);
Type tDelegate = ev.EventHandlerType;
MethodInfo miHandler = typeof(CustomBox).GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance);
Delegate d = Delegate.CreateDelegate(tDelegate, this, miHandler);
ev.AddEventHandler(this,d);
}
[CustomAttribute(EventName = "PreviewTextInput",Value = 2)]
private void CustomBox_TextChanged(object sender, TextCompositionEventArgs e)
{
this.Text = e.Text;
e.Handled = true;
}
[CustomAttribute(EventName = "PreviewTextInput", Value = 1)]
private void CustomBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
if (e.Text.Contains("e"))
{
e.Handled = true;
}
else e.Handled = false;
}
}
以上,即使有人創建
this.PreviewTextInput + = CustomBox_TextChanged; 處理程序操縱 文本框文本並將其更改爲不願意的文本並通過e.handle = true阻止另一個事件;
就在this.PreviewTextInput + = CustomBox_PreviewTextInput; 反映根據您的定義更改他們的訂單。
如果您創建自定義TextBox,然後使用它,然後創建PreviewTextInput,那麼它將在您正在擴展的TextBox內部運行之前運行。我預覽了預覽鍵在PreviewTextInput之前運行,你可以在那裏處理它。 – adminSoftDK