public WeakFunc(object?target, Func <TResult> method, bool keepTargetAlive = false) : base(target, method, keepTargetAlive) { if (method.Target == null) { // Static delegate. return; } var type = method.Target.GetType(); var key = new DelegateKey(type, method.Method); this.compiledDelegate = delegateCache[key] as Func <object, TResult>; if (this.compiledDelegate == null) { var targetParam = Expression.Parameter(typeof(object)); this.compiledDelegate = Expression.Lambda <Func <object, TResult> >( Expression.Call( Expression.Convert(targetParam, type), method.Method), targetParam) .CompileFast(); lock (delegateCache) { delegateCache[key] = this.compiledDelegate; } } }
public static Type NewDelegateType(Type ret, Type[] parameters) { var key = new DelegateKey(ret, (Type[])parameters.Clone()); Type delegateType; if (!DelegateTypes.TryGetValue(key, out delegateType)) { var assemblyName = Guid.NewGuid().ToString(); var name = new AssemblyName(assemblyName); var assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(name, AssemblyBuilderAccess.Run); var moduleBuilder = assemblyBuilder.DefineDynamicModule(name.Name); assemblyBuilder.DefineVersionInfoResource(); var typeBuilder = moduleBuilder.DefineType("CustomDelegate", System.Reflection.TypeAttributes.Public | System.Reflection.TypeAttributes.Sealed | System.Reflection.TypeAttributes.AutoClass, typeof(MulticastDelegate)); var constructor = typeof(UnmanagedFunctionPointerAttribute).GetConstructors()[0]; // Make sure that we setup the C calling convention on the unmanaged delegate var customAttribute = new CustomAttributeBuilder(constructor, new object[] { CallingConvention.Cdecl }); typeBuilder.SetCustomAttribute(customAttribute); typeBuilder.DefineConstructor(System.Reflection.MethodAttributes.Public | System.Reflection.MethodAttributes.HideBySig | System.Reflection.MethodAttributes.RTSpecialName, CallingConventions.Standard, _DelegateCtorSignature).SetImplementationFlags(System.Reflection.MethodImplAttributes.CodeTypeMask); typeBuilder.DefineMethod("Invoke", System.Reflection.MethodAttributes.Public | System.Reflection.MethodAttributes.Virtual | System.Reflection.MethodAttributes.HideBySig | System.Reflection.MethodAttributes.VtableLayoutMask, ret, parameters).SetImplementationFlags(System.Reflection.MethodImplAttributes.CodeTypeMask); delegateType = typeBuilder.CreateType(); DelegateTypes.Add(key, delegateType); } return(delegateType); }
static void DelegateAdapter(DelegateKey key, SB SB, string suffix) { System.Text.StringBuilder sb = null; bool isVoid = key.returnType.Name == "Void"; var ps = key.parameters; if (isVoid) { sb = SB.RegisterMethodDelegate; sb.Append(suffix); sb.Append("appdomain.DelegateManager.RegisterMethodDelegate<"); } else { sb = SB.RegisterFunctionDelegate; sb.Append(suffix); sb.Append("appdomain.DelegateManager.RegisterFunctionDelegate<"); } bool first = true; foreach (var i in ps) { if (first) { first = false; } else { sb.Append(", "); } string clsName; string realClsName; bool isByRef; GetClassName(i, out clsName, out realClsName, out isByRef); sb.Append(realClsName); } if (!isVoid) { if (ps.Count != 0) { sb.Append(", "); } string clsName; string realClsName; bool isByRef; GetClassName(key.returnType, out clsName, out realClsName, out isByRef); sb.Append(realClsName); } sb.Append(">"); sb.Append("();"); sb.AppendLine(); }
private void RemoveDelegate(IDto objectToInvoke, EventInfo ei) { var signature = new DelegateKey(objectToInvoke.DtoGuid, ei.Name); lock (((IDictionary)_delegates).SyncRoot) { var delegateToRemove = _delegates[signature]; if (!_delegates.Remove(signature)) { return; } ei.RemoveEventHandler(objectToInvoke, delegateToRemove); Debug.WriteLine($"Server: removed delegate {ei.Name} on {objectToInvoke}"); } }
private void AddDelegate(IDto objectToInvoke, EventInfo ei) { var signature = new DelegateKey(objectToInvoke.DtoGuid, ei.Name); lock (((IDictionary)_delegates).SyncRoot) { if (_delegates.ContainsKey(signature)) { return; } var delegateToInvoke = ConvertDelegate((Action <object, EventArgs>) delegate(object o, EventArgs ea) { NotifyClient(o, ea, ei.Name); }, ei.EventHandlerType); Debug.WriteLine($"Server: added delegate {ei.Name} on {objectToInvoke}"); _delegates[signature] = delegateToInvoke; ei.AddEventHandler(objectToInvoke, delegateToInvoke); } }
static void BuildDelegate(Dictionary <System.Type, UseTypeInfo> csharpDelegate, Dictionary <System.Type, UseTypeInfo> hotTDic) { string marco, file_suffix; GetPlatform(out marco, out file_suffix); string file_path = string.Format("Assets/Model/XIL/Auto/ILRegType_{0}.cs", file_suffix); Dictionary <DelegateKey, List <System.Type> > keys = new Dictionary <DelegateKey, List <System.Type> >(); foreach (var ator in csharpDelegate) { var d = ator.Key; var method = d.GetMethod("Invoke"); if (method == null) { wxb.L.LogErrorFormat("method:{0}", d.FullName); continue; } var dk = new DelegateKey(method); List <System.Type> ds = null; if (!keys.TryGetValue(dk, out ds)) { ds = new List <System.Type>(); keys.Add(dk, ds); } ds.Add(d); } SB sb = new SB(); //System.Text.StringBuilder RegisterFunctionDelegate = new System.Text.StringBuilder(); //System.Text.StringBuilder RegisterDelegateConvertor = new System.Text.StringBuilder(); //System.Text.StringBuilder RegisterMethodDelegate = new System.Text.StringBuilder(); foreach (var ator in keys) { if (ator.Key.isReg) { //sb.Append(suffix); DelegateAdapter(ator.Key, sb, suffix, (ssb) => { //foreach (var v in ator.Value) //{ // ssb.Append(suffix); // ssb.Append("// "); // ssb.AppendLine(GetClassRealClsName(v)); // UseTypeInfo info = null; // if (csharpDelegate.TryGetValue(v, out info)) // { // if (info.infos.Count == 0) // continue; // info.To(ssb, suffix); // } //} }); //sb.AppendLine(); } foreach (var v in ator.Value) { if (IsSystemDelegate(v)) { continue; } ConvertToDelegate(v, sb.RegisterDelegateConvertor, suffix); sb.RegisterDelegateConvertor.AppendLine(); } //sb.AppendLine(); } System.Text.StringBuilder tsb = new System.Text.StringBuilder(); BuildDHot(hotTDic, tsb); System.IO.Directory.CreateDirectory(file_path.Substring(0, file_path.LastIndexOf('/'))); System.IO.File.WriteAllText(file_path, string.Format(RegTextFile, marco, sb.RegisterFunctionDelegate, sb.RegisterDelegateConvertor, sb.RegisterMethodDelegate, tsb.ToString()), System.Text.Encoding.UTF8); UnityEditor.AssetDatabase.ImportAsset(file_path); UnityEditor.AssetDatabase.Refresh(); }
static void BuildDelegate(Dictionary <System.Type, UseTypeInfo> csharpDelegate, Dictionary <System.Type, UseTypeInfo> hotTDic) { string marco, file_suffix; GetPlatform(out marco, out file_suffix); string file_path = string.Format("Assets/XIL/Auto/ILRegType_{0}.cs", file_suffix); Dictionary <DelegateKey, List <System.Type> > keys = new Dictionary <DelegateKey, List <System.Type> >(); foreach (var ator in csharpDelegate) { var d = ator.Key; if (d.IsAbstract) { continue; } var method = d.GetMethod("Invoke"); if (method == null) { wxb.L.LogErrorFormat("method:{0}", d.FullName); continue; } var dk = new DelegateKey(method); List <System.Type> ds = null; if (!keys.TryGetValue(dk, out ds)) { ds = new List <System.Type>(); keys.Add(dk, ds); } ds.Add(d); } SB sb = new SB(); //System.Text.StringBuilder RegisterFunctionDelegate = new System.Text.StringBuilder(); //System.Text.StringBuilder RegisterDelegateConvertor = new System.Text.StringBuilder(); //System.Text.StringBuilder RegisterMethodDelegate = new System.Text.StringBuilder(); foreach (var ator in keys) { string error; if (ator.Key.isReg(out error)) { //sb.Append(suffix); DelegateAdapter(ator.Key, sb, suffix, (ssb) => { #if DEBUG_INPUT foreach (var v in ator.Value) { DebugInfo(suffix, v, csharpDelegate, ssb); } #endif }); //sb.AppendLine(); } else { if (error != null) { System.Text.StringBuilder esb = new System.Text.StringBuilder(); esb.AppendLine(error); foreach (var v in ator.Value) { esb.AppendLine(GetClassRealClsName(v)); csharpDelegate[v].To(esb, ""); } L.LogErrorFormat(esb.ToString()); } continue; } foreach (var v in ator.Value) { if (IsSystemDelegate(v)) { continue; } #if DEBUG_INPUT var ssb = sb.RegisterDelegateConvertor; DebugInfo(suffix, v, csharpDelegate, ssb); #endif ConvertToDelegate(v, sb.RegisterDelegateConvertor, suffix); sb.RegisterDelegateConvertor.AppendLine(); } //sb.AppendLine(); } System.Text.StringBuilder tsb = new System.Text.StringBuilder(); BuildDHot(hotTDic, tsb); System.IO.Directory.CreateDirectory(file_path.Substring(0, file_path.LastIndexOf('/'))); System.IO.File.WriteAllText(file_path, string.Format(RegTextFile, marco, sb.RegisterFunctionDelegate, sb.RegisterDelegateConvertor, sb.RegisterMethodDelegate, tsb.ToString()), System.Text.Encoding.UTF8); UnityEditor.AssetDatabase.ImportAsset(file_path); UnityEditor.AssetDatabase.Refresh(); }
static void BuildDelegate(Dictionary <System.Type, UseTypeInfo> csharpDelegate, Dictionary <System.Type, UseTypeInfo> hotTDic) { string file_path = string.Format("Assets/ZJY_Framework/ILRuntime/Generated/ILRegType.cs"); Dictionary <DelegateKey, List <System.Type> > keys = new Dictionary <DelegateKey, List <System.Type> >(); foreach (var ator in csharpDelegate) { var d = ator.Key; var method = d.GetMethod("Invoke"); if (method == null) { UnityEngine.Debug.LogErrorFormat("method:{0}", d.FullName); continue; } var dk = new DelegateKey(method); List <System.Type> ds = null; if (!keys.TryGetValue(dk, out ds)) { ds = new List <System.Type>(); keys.Add(dk, ds); } ds.Add(d); } SB sb = new SB(); foreach (var ator in keys) { if (ator.Key.isReg) { //sb.Append(suffix); DelegateAdapter(ator.Key, sb, suffix); //sb.AppendLine(); } foreach (var v in ator.Value) { if (IsSystemDelegate(v)) { continue; } ConvertToDelegate(v, sb.RegisterDelegateConvertor, suffix); sb.RegisterDelegateConvertor.AppendLine(); } //sb.AppendLine(); } System.IO.Directory.CreateDirectory(file_path.Substring(0, file_path.LastIndexOf('/'))); System.IO.File.WriteAllText(file_path, string.Format(RegTextFile, sb.RegisterFunctionDelegate, sb.RegisterDelegateConvertor, sb.RegisterMethodDelegate), System.Text.Encoding.UTF8); UnityEditor.AssetDatabase.ImportAsset(file_path); }