をインスタンス化された機能を所有しているオブジェクトの前にdelegate`機能in割り当て `私は正しいタイプの<code>Event</code>が渡された場合<code>Watcher</code>が<code>Event</code>に興味があると<code>Action</code>を起動するように設定することができるシステムに取り組んでいます
Action
には、起動時にEvent
に関する情報が必要です。
以下のコードでは、Watcher
を設定するときに、イベント機能を渡した代理人をAction
プロパティ(.NETのファクトではなく、フィールド/メンバー/その他)として使用します。
もちろんコードの問題は、// Step3にあるとき、私がそうすると、フィールドTheEvent
はヌルです。 Watcher
にEvent
が渡されると、それは判明します。私はそれがEvent
が割り当てられる前にEvent
から必要なプロパティを取得Action
だ作るためにWatcher
を伝えることができますどのように
?
namespace ConsoleApplication1
{
using System;
internal class Program
{
static void Main(string[] args)
{
//Step1: choose the IEvent: NumericEvent
//Step2: create Watcher<NumbericEvent>
Watcher<NumericEvent> aWatcher = new Watcher<NumericEvent>();
//Step3: Choose and add an action
aWatcher.TheAction = new AlphaAction();
//Step3 bind the ActionProperties
aWatcher.TheAction.AnActionPropertyA = aWatcher.TheEvent.GetProperty1;
aWatcher.TheAction.AnActionPropertyB = aWatcher.TheEvent.GetProperty2;
//Step4 Bind the event
NumericEvent anEvent = new NumericEvent();
aWatcher.Call(anEvent);
}
}
internal class AlphaAction
{
internal delegate string ActionPropertyA();
internal delegate int ActionPropertyB();
internal ActionPropertyA AnActionPropertyA;
internal ActionPropertyB AnActionPropertyB;
internal string ActionPropertyC;
public void Run()
{
Console.Write(AnActionPropertyA.Invoke());
Console.Write(AnActionPropertyB.Invoke());
}
}
internal interface IEvent
{
}
internal class NumericEvent:IEvent
{
internal string EventProperty1 = "Property1";
internal int EventProperty2 = 2;
internal string GetProperty1()
{
return EventProperty1;
}
internal int GetProperty2()
{
return EventProperty2;
}
}
internal class Watcher<T> where T:IEvent
{
internal NumericEvent TheEvent;
internal AlphaAction TheAction;
internal void Call(IEvent anEvent)
{
TheEvent = (NumericEvent)anEvent;
TheAction.Run();
}
}
}
おそらく完全に異なるアプローチをとる必要がありますが、私はこれが問題とその結果を望むことを望みます。
UPDATE
私はDelegate.CreateDelegate(https://msdn.microsoft.com/en-us/library/s3860fy3.aspx)を使用して、いくつかのprogerssを作ったが、nullの場合は割り当てられたとき、それは静的として扱いだとして、それはまだかなり右ではありません。
名前空間をConsoleApplication1 { using System; using System.Reflection;私はよくして、あなたがGalasoft's MVVM Light Toolkitを利用することができますあなたの必要性を理解していれば
internal class Program
{
static void Main(string[] args)
{
//Step1: choose the IEvent: NumericEvent
//Step2: create Watcher<NumbericEvent>
Watcher<NumericEvent> aWatcher = new Watcher<NumericEvent>();
//Step3: Choose and add an action
aWatcher.TheAction = new AlphaAction();
//Step3 bind the ActionProperties
MethodInfo method1 = typeof(NumericEvent).GetMethod("GetProperty1");
aWatcher.TheAction.AnActionPropertyA = (AlphaAction.ActionPropertyA)Delegate.CreateDelegate(typeof(AlphaAction.ActionPropertyA), aWatcher.TheEvent, method1);
MethodInfo method2 = typeof(NumericEvent).GetMethod("GetProperty2");
aWatcher.TheAction.AnActionPropertyB = (AlphaAction.ActionPropertyB)Delegate.CreateDelegate(typeof(AlphaAction.ActionPropertyB), aWatcher.TheEvent, method2);
//Step4 an event is created and passed to the Watcher
NumericEvent anEvent = new NumericEvent("bla",3);
if (aWatcher.GType() == anEvent.GetType())
aWatcher.Call(anEvent);
}
}
internal abstract class BaseAction
{
public abstract void Run();
}
internal class AlphaAction: BaseAction
{
internal delegate string ActionPropertyA();
internal delegate int ActionPropertyB();
internal ActionPropertyA AnActionPropertyA;
internal ActionPropertyB AnActionPropertyB;
internal string ActionPropertyC;
public override void Run()
{
Console.Write(AnActionPropertyA.Invoke());
Console.Write(AnActionPropertyB.Invoke());
}
}
internal interface IEvent
{
}
internal class NumericEvent : IEvent
{
private readonly string _eventProperty1;
private readonly int _eventProperty2;
public NumericEvent(string p1, int p2)
{
_eventProperty1 = p1;
_eventProperty2 = p2;
}
public string GetProperty1()
{
return _eventProperty1;
}
public int GetProperty2()
{
return _eventProperty2;
}
}
internal class Watcher<T> where T:IEvent
{
internal T TheEvent;
internal AlphaAction TheAction;
internal Type GType()
{
return typeof(T);
}
internal void Call(IEvent anEvent)
{
TheEvent = (T)anEvent;
TheAction.Run();
}
}
}
私の主なポイントは、イベントに依存するアクションを持っていないが、ウォッチャーは正しいイベントプロパティをアクションに結び付けることです。ウォッチャー(したがって同じイベント)に対して複数のアクション(アクションのサブクラス実装が異なる)が存在し、同じアクションが異なるウォッチャーによって使用される可能性があります。つまり、異なるイベントで呼び出すことができます。したがって、アクションはイベントから完全に分離する必要があります。このアクションは、値を取得する方法を知る必要があるだけです。 – EasierSaidThanDone
'Delegate.CreateDelegate'を使用して解決策に近づいていますが、まだそれほど正しくありません....' MethodInfo method1 = typeof(NumericEvent).GetMethod( "GetProperty1" ); \t \t \t aWatcher.TheAction.AnActionPropertyA =(AlphaAction.ActionPropertyA)Delegate.CreateDelegate(typeof演算(AlphaAction.ActionPropertyA)、aWatcher.TheEvent、法1); ' – EasierSaidThanDone
だから' aWatcher.TheEvent'が時間 'createDelegate'にヌルでありますデリゲート 'AnActionPropertyA'が呼び出されたときには呼び出されますが、呼び出されません。 - >割り当てられた 'aWatcher 'を使用する方法はありますか?TheEvent'? – EasierSaidThanDone