Last active
December 27, 2017 20:48
-
-
Save shirothin/ef6a8afd9b273fb20ed93771208105cd to your computer and use it in GitHub Desktop.
カスタムEventArgs ref: https://qiita.com/shirothin@github/items/13f38bd72193ad885eb9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AtepEventArgs<T> : EventArgs | |
{ | |
public T Value { get; private set; } | |
public AtepEventArgs(T val) | |
{ | |
Value = val; | |
} | |
} | |
public delegate void EventHandler<TSender, TEventArgs>(TSender sender, TEventArgs e); | |
public interface ILauncher<T> | |
{ | |
event EventHandler<object, AtepEventArgs<T>> Raise; | |
} | |
public class AtepLauncher<T> : ILauncher<T> | |
{ | |
private T arg = default(T); | |
public AtepLauncher() | |
{ | |
} | |
public AtepLauncher(T a) | |
{ | |
this.arg = a; | |
} | |
public T Arg | |
{ | |
get | |
{ | |
return this.arg; | |
} | |
set | |
{ | |
this.arg = value; | |
OnChanged(this, new AtepEventArgs<T>(this.arg)); | |
} | |
} | |
// Delegate | |
public event EventHandler<object, AtepEventArgs<T>> Raise; | |
// Trigger | |
public void OnChanged(object sender, AtepEventArgs<T> e) | |
{ | |
if (Raise != null) | |
{ | |
// Call the Event | |
Raise(this, e); | |
} | |
} | |
} | |
public class AtepContainer<T> | |
{ | |
private List<ILauncher<T>> launcher; | |
public AtepContainer() | |
{ | |
this.launcher = new List<ILauncher<T>>(); | |
} | |
public void Add(ILauncher<T> lchr) | |
{ | |
lchr.Raise += new EventHandler<object, AtepEventArgs<T>>(HandleChanged); | |
this.launcher.Add(lchr); | |
} | |
internal virtual void HandleChanged(object sender, AtepEventArgs<T> e) | |
{ | |
// イベント発火, ここにoverride メソッドを記述 | |
Console.WriteLine("Received event. value={0}", e.Value); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// (目的の)型 | |
/// </summary> | |
public class MyItem | |
{ | |
public MyItem() | |
{ | |
} | |
public MyItem(int n, string s) | |
{ | |
Number = n; | |
TextMessage = s; | |
} | |
public int Number { get; set; } | |
public string TextMessage { get; set; } | |
} | |
/// <summary> | |
/// (目的の)メイン | |
/// </summary> | |
internal class Test | |
{ | |
private static Test _instance { get; set; } | |
public static Test GetInstance() | |
{ | |
return _instance; | |
} | |
private static void Main(string[] args) | |
{ | |
var myLauncher = new AtepLauncher<MyItem>(); | |
var container = new MyContainer(); | |
container.Add(myLauncher); | |
myLauncher.Arg = new MyItem(1, "Item1"); | |
// Keep the console window open in debug mode. | |
System.Console.WriteLine("Press any key to exit."); | |
System.Console.ReadKey(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Threading; | |
/// <summary> | |
/// Note : | |
/// Atep = Arbitary-Triggered Eventhandler of Pluripotency | |
/// </summary> | |
namespace RaiseBaseClassEventsTestPart3 | |
{ | |
public class AtepEventArgs<T> : EventArgs | |
{ | |
public T Value { get; private set; } | |
public AtepEventArgs(T val) | |
{ | |
Value = val; | |
} | |
} | |
public delegate void EventHandler<TSender, TEventArgs>(TSender sender, TEventArgs e); | |
public interface ILauncher<T> | |
{ | |
event EventHandler<object, AtepEventArgs<T>> Raise; | |
} | |
public class AtepLauncher<T> : ILauncher<T> | |
{ | |
private T arg = default(T); | |
public AtepLauncher() | |
{ | |
} | |
public AtepLauncher(T a) | |
{ | |
this.arg = a; | |
} | |
public T Arg | |
{ | |
get | |
{ | |
return this.arg; | |
} | |
set | |
{ | |
this.arg = value; | |
OnChanged(this, new AtepEventArgs<T>(this.arg)); | |
} | |
} | |
// Delegate | |
public event EventHandler<object, AtepEventArgs<T>> Raise; | |
// Trigger | |
public void OnChanged(object sender, AtepEventArgs<T> e) | |
{ | |
if (Raise != null) | |
{ | |
// Call the Event | |
Raise(this, e); | |
} | |
} | |
} | |
public class AtepContainer<T> | |
{ | |
private List<ILauncher<T>> launcher; | |
public AtepContainer() | |
{ | |
this.launcher = new List<ILauncher<T>>(); | |
} | |
public void Add(ILauncher<T> lchr) | |
{ | |
lchr.Raise += new EventHandler<object, AtepEventArgs<T>>(HandleChanged); | |
this.launcher.Add(lchr); | |
} | |
internal virtual void HandleChanged(object sender, AtepEventArgs<T> e) | |
{ | |
// イベント発火, ここにoverride メソッドを記述 | |
Console.WriteLine("Received event. value={0}", e.Value); | |
} | |
} | |
/// <summary> | |
/// (目的の) ランチャコンテナ | |
/// </summary> | |
public class MyContainer : AtepContainer<MyItem> | |
{ | |
internal override void HandleChanged(object sender, AtepEventArgs<MyItem> e) | |
{ | |
if (e.Value == null) | |
return; | |
var item = e.Value as MyItem; | |
var s = string.Format("({0}, {1})", item.Number.ToString(), item.TextMessage); | |
Console.WriteLine("Received event." + s); | |
} | |
} | |
/// <summary> | |
/// (目的の)型 | |
/// </summary> | |
public class MyItem | |
{ | |
public MyItem() | |
{ | |
} | |
public MyItem(int n, string s) | |
{ | |
Number = n; | |
TextMessage = s; | |
} | |
public int Number { get; set; } | |
public string TextMessage { get; set; } | |
} | |
/// <summary> | |
/// (目的の)メイン | |
/// </summary> | |
internal class Test | |
{ | |
private static Test _instance { get; set; } | |
public static Test GetInstance() | |
{ | |
return _instance; | |
} | |
private static void Main(string[] args) | |
{ | |
var myLauncher = new AtepLauncher<MyItem>(); | |
var container = new MyContainer(); | |
container.Add(myLauncher); | |
myLauncher.Arg = new MyItem(1, "Item1"); | |
// Keep the console window open in debug mode. | |
System.Console.WriteLine("Press any key to exit."); | |
System.Console.ReadKey(); | |
} | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Launcher<T> : BaseLauncher<T> | |
{ | |
private T _obj = default(T); | |
public Launcher() | |
{ | |
} | |
public Launcher(T obj) | |
{ | |
_obj = obj; | |
} | |
public void Trigger(object sender, T obj) | |
{ | |
_obj = obj; | |
Thread worker = new Thread(work); | |
worker.Start(); | |
} | |
private void work() | |
{ | |
OnChanged(new OwnEventArgs<T>(_obj)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LaucherContainer<T> | |
{ | |
private List<ILauncher<T>> _launcher; | |
public LaucherContainer() | |
{ | |
_launcher = new List<ILauncher<T>>(); | |
} | |
public void Add(ILauncher<T> launcher) | |
{ | |
launcher.Raised += new EventHandler<object, OwnEventArgs<T>>(HandleChanged); | |
_launcher.Add(launcher); | |
} | |
internal virtual void HandleChanged(object sender, OwnEventArgs<T> e) | |
{ | |
// イベント発火, ここにoverride メソッドを記述 | |
Console.WriteLine("Received event. value={0}", e.Value); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ValueContainer : LaucherContainer<object> | |
{ | |
internal override void HandleChanged(object sender, OwnEventArgs<object> e) | |
{ | |
Console.WriteLine("Received event. value={0}", e.Value); | |
} | |
} | |
public class OwnContainer : LaucherContainer<OwnItem> | |
{ | |
internal override void HandleChanged(object sender, OwnEventArgs<OwnItem> e) | |
{ | |
if (e.Value == null) | |
return; | |
var item = e.Value as OwnItem; | |
var s = string.Format("({0}, {1})", item.Number.ToString(), item.TextMessage); | |
Console.WriteLine("Received event." + s); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class OwnItem | |
{ | |
public OwnItem(int n, string s) | |
{ | |
Number = n; | |
TextMessage = s; | |
} | |
public int Number {get; set;} | |
public string TextMessage {get; set;} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal class Test | |
{ | |
private static Test _instance { get; set; } | |
public static Test GetInstance() | |
{ | |
return _instance; | |
} | |
private static void Main(string[] args) | |
{ | |
var launcher1 = new Launcher<object>(); | |
var launcher2 = new Launcher<OwnItem>(); | |
var container1 = new ValueContainer(); | |
var container2 = new OwnContainer(); | |
container1.Add(launcher1); | |
container2.Add(launcher2); | |
launcher1.Trigger(GetInstance(), 16384); | |
launcher2.Trigger(GetInstance() ,new OwnItem(30, "Item30")); | |
// Keep the console window open in debug mode. | |
System.Console.WriteLine("Press any key to exit."); | |
System.Console.ReadKey(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Threading; | |
namespace RaiseBaseClassEventsTestPart2 | |
{ | |
//public delegate void EventHandler<T>(OwnEventArgs<T> e); | |
public delegate void EventHandler<TSender, TEventArgs>(TSender sender, TEventArgs e); | |
public class OwnEventArgs<T> : EventArgs | |
{ | |
public T Value { get; private set; } | |
public OwnEventArgs(T val) | |
{ | |
Value = val; | |
} | |
} | |
public interface ILauncher<T> | |
{ | |
event EventHandler<object, OwnEventArgs<T>> Raised; | |
} | |
public abstract class BaseLauncher<T> : ILauncher<T> | |
{ | |
public event EventHandler<object, OwnEventArgs<T>> Raised; | |
protected virtual void OnChanged(OwnEventArgs<T> e) | |
{ | |
if (Raised != null) | |
{ | |
Raised(this, e); | |
} | |
} | |
} | |
public class Launcher<T> : BaseLauncher<T> | |
{ | |
private T _obj = default(T); | |
public Launcher() | |
{ | |
} | |
public Launcher(T obj) | |
{ | |
_obj = obj; | |
} | |
public void Trigger(object sender, T obj) | |
{ | |
_obj = obj; | |
Thread worker = new Thread(work); | |
worker.Start(); | |
} | |
private void work() | |
{ | |
OnChanged(new OwnEventArgs<T>(_obj)); | |
} | |
} | |
public class LaucherContainer<T> | |
{ | |
private List<ILauncher<T>> _launcher; | |
public LaucherContainer() | |
{ | |
_launcher = new List<ILauncher<T>>(); | |
} | |
public void Add(ILauncher<T> launcher) | |
{ | |
launcher.Raised += new EventHandler<object, OwnEventArgs<T>>(HandleChanged); | |
_launcher.Add(launcher); | |
} | |
internal virtual void HandleChanged(object sender, OwnEventArgs<T> e) | |
{ | |
// イベント発火, ここにoverride メソッドを記述 | |
Console.WriteLine("Received event. value={0}", e.Value); | |
} | |
} | |
/// <summary> | |
///(目的の) EventContainerのカスタム版 | |
/// </summary> | |
public class ValueContainer : LaucherContainer<object> | |
{ | |
internal override void HandleChanged(object sender, OwnEventArgs<object> e) | |
{ | |
Console.WriteLine("Received event. value={0}", e.Value); | |
} | |
} | |
public class OwnContainer : LaucherContainer<OwnItem> | |
{ | |
internal override void HandleChanged(object sender, OwnEventArgs<OwnItem> e) | |
{ | |
if (e.Value == null) | |
return; | |
var item = e.Value as OwnItem; | |
var s = string.Format("({0}, {1})", item.Number.ToString(), item.TextMessage); | |
Console.WriteLine("Received event." + s); | |
} | |
} | |
/// <summary> | |
/// (目的の)型 | |
/// </summary> | |
public class OwnItem | |
{ | |
public OwnItem(int n, string s) | |
{ | |
Number = n; | |
TextMessage = s; | |
} | |
public int Number { get; set; } | |
public string TextMessage { get; set; } | |
} | |
/// <summary> | |
/// (目的の)メイン | |
/// </summary> | |
internal class Test | |
{ | |
private static Test _instance { get; set; } | |
public static Test GetInstance() | |
{ | |
return _instance; | |
} | |
private static void Main(string[] args) | |
{ | |
var launcher1 = new Launcher<object>(); | |
var launcher2 = new Launcher<OwnItem>(); | |
var container1 = new ValueContainer(); | |
var container2 = new OwnContainer(); | |
container1.Add(launcher1); | |
container2.Add(launcher2); | |
launcher1.Trigger(GetInstance(), 16384); | |
launcher2.Trigger(GetInstance() ,new OwnItem(30, "Item30")); | |
// Keep the console window open in debug mode. | |
System.Console.WriteLine("Press any key to exit."); | |
System.Console.ReadKey(); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment