private void DoInterval(object obj) { if (lstNic.InvokeRequired) { BaseDelegate bd = new BaseDelegate(DoInterval); this.Invoke(bd, obj); return; } else { lstNic.Items.Clear(); Win32Native.MIB_IF_TABLE2 s_Mit2 = GetMIB2Interface(); for (int i = 0; i < s_Mit2.NumEntries; i++) { lstNic.Items.Add( new ListViewItem( new string[] { s_Mit2.Table[i].Description, s_Mit2.Table[i].Alias, s_Mit2.Table[i].InOctets.ToString(), s_Mit2.Table[i].OutOctets.ToString() } ) ); } } }
public static void TestDelegates() { BaseDelegate dlgB = new BaseDelegate(GetBase); Base b = dlgB(); BaseDelegate dlgD = new BaseDelegate(GetDerived); Derived d = dlgD() as Derived; //Derived d = dlgD(); }
private void IntervalListen() { string group = "ListenNIC"; QuartzJob job = new QuartzJob(group); BaseDelegate bd = new BaseDelegate(DoInterval); //job.CreateJobWithParam(,) int interval = 30; int repeact = 24 * 60 * 60 / interval; job.CreateJobWithParam <JobDelegateFunction>(new object[] { bd }, DateTime.Now, interval, repeact); }
// Update is called once per frame void Update() { //當前狀態在類別內,且對應的委託事件存在,則執行對應狀態事件 if (currentState < states.Count) { BaseDelegate state = states [currentState]; if (state != null) { state(); } } }
void CallGetVerifyKey(object obj) { if (lstProcess.InvokeRequired) { BaseDelegate bd = new BaseDelegate(CallGetVerifyKey); this.Invoke(bd, obj); // CallGetVerifyKey(obj); return; } object[] o = obj as object[]; GetVerifyKey((WebChatLoginTocken)o[0], (string)o[1], (CookieContainer)o[2]); }
void NewThreadDoQueryMsg(object param) { //object[] ps = param as object[]; //WebChatLoginTocken tocken = ps[0] as WebChatLoginTocken; //string deviceID = ps[1] as string; //object[] funParam = new object[] { tocken, deviceID }; QuartzJob job = new QuartzJob(); BaseDelegate bd = new BaseDelegate(CallGetVerifyKey); object[] quartParam = new object[] { bd, param }; job.CreateJobWithParam <JobDelegate <DADefineHelper> >(quartParam, DateTime.Now, 2, -1); }
public void Execute(IJobExecutionContext context) { JobDataMap data = context.JobDetail.JobDataMap; object objParam = data.Get(QuartzJob.QuartzParam);//提取委托及对应的参数 object[] ps = objParam as object[]; if (ps.Length == 0) {//没有传递参数 return; } BaseDelegate bd = ps[0] as BaseDelegate; object obj = ps.Length >= 2 ? ps[1] : null; bd(obj); }
/// <summary> /// Initializes the preset with the specified visiblity. The preset must be initialized before the preset values are applied so the delegates can be created. /// </summary> /// <param name="obj">The object to map the delegates to.</param> /// <param name="visibility">Specifies the visibility of the field/properties that should be retrieved.</param> public override void Initialize(object obj, MemberVisibility visibility) { m_Delegates = new BaseDelegate[m_Data.ValueHashes.Length]; var valuePositionMap = new Dictionary <int, int>(m_Data.ValueHashes.Length); for (int i = 0; i < m_Data.ValueHashes.Length; ++i) { valuePositionMap.Add(m_Data.ValueHashes[i], i); } var valueCount = 0; var properties = Serialization.GetSerializedProperties(obj.GetType(), visibility); for (int i = 0; i < properties.Length; ++i) { var hash = Serialization.StringHash(properties[i].PropertyType.FullName) + Serialization.StringHash(properties[i].Name); int position; if (!valuePositionMap.TryGetValue(hash, out position)) { continue; } // Create a generic delegate based on the property type. var genericDelegateType = typeof(GenericDelegate <>).MakeGenericType(properties[i].PropertyType); m_Delegates[valueCount] = Activator.CreateInstance(genericDelegateType) as BaseDelegate; // Initialize the delegate. if (m_Delegates[valueCount] != null) { m_Delegates[valueCount].Initialize(obj, properties[i], valuePositionMap, m_Data, visibility); } else { Debug.LogWarning("Warning: Unable to create preset of type " + properties[i].PropertyType); } valueCount++; } // The delegate length may not match if a property has been added but no longer exists. if (m_Delegates.Length != valueCount) { Array.Resize(ref m_Delegates, valueCount); } }
// Main static void Main(string[] args) { // 1 var program = new Program(); var printer = new Printer(); BaseDelegate aa = Gav; BaseDelegate2 bb = Gav2; // 2 DelegateForStatic delegateForStatic = StaticMethod; delegateForStatic("Static method works"); // 3 Console.WriteLine(); BaseDelegate2 printerDelegate = printer.Print; printerDelegate.Invoke(new B()); // 4 var del = new Action <string>(str => Console.Write(str)); del("adsfdsfdsdfsdf"); del?.Invoke("adsfdsfdsdfsdf"); // 5 Console.WriteLine(); DelegateForChains d1 = ChainMethod2; DelegateForChains d2 = ChainMethod1; DelegateForChains d3 = ChainMethod1; d3 += d1; d3 += d2; d3 += d2; d3 -= d2; d3(); // 6 var dels = d3.GetInvocationList(); Console.WriteLine(); foreach (var d in dels) { if (nameof(ChainMethod1) == d.Method.Name) { d.DynamicInvoke(); } } // 7 Console.WriteLine(); Func <double, double, double> genDel = (a, b) => Math.Pow(a, b); Console.WriteLine(genDel(2, 3)); PowDelegate powDel = (a, b) => Math.Pow(a, b); Console.WriteLine(powDel(3, 2)); // 9 Console.WriteLine(); MyEvent += EventMethod; MyEvent(); }
// Constructor public MyList(BaseDelegate mStrategy) { this.strategy = mStrategy; }