從回調的角度來看,當我知道myDelegate.Target包含對其所包含方法的類的引用時,我面臨一種奇怪的情況。 (我搜索它SO,但我原諒,如果我錯過了一些線程已經回答這個問題)如何確保委託實例引用
例如
public delegate void TravePlanDelegate();
public class Traveller
{
//Papa is planing a tour, along with Mama
public void Planner()
{
//Asking me (delegate) to hold a letter about PlanA's detail
TravelPlanDelegate myPlan = PlanA;
//Sending me to TravelAgency office with letter
new TravelAgency().ExecuteTravelPlan(myPlan);
}
public void PlanA()
{
//Papa's open plan with Mama
Console.WriteLine("First Berline, then New Yark and finally Lahore");
}
public void PlanB()
{
//Papa's secret plan
Console.WriteLine("First Dubai, and then Lahore");
}
}
public class TravelAgency
{
public void ExecuteTravelPlan(TravePlanDelegate tp)
{
Traveller traveller = (Traveller)tp.Target;
//Here it should execute plan
//tp.Target - A reference to Traveler class, which can lead travel
//agency to Papa's secret plan (And exposes it to Mama)
}
}
在這個例子中,旅行社可以獲取有關爸爸的祕密計劃從代表的信息了。我是否正確地獲得委託概念或錯過了什麼?
在您的過程中是否存在潛在的敵對.NET代碼?或者你想防止泄漏的API內部? – usr
我想使用像TravelAgency這樣的圖書館,但我懷疑他們可以進入我自己的圖書館的細節。 – Lali