public static bool TryCreateFromExpression <TDelegate>( string expressionText, string className, string methodName, string returnTypeName, Type returnType, string parameterTypeName, Type parameterType, string parameterName, [NotNullWhen(true)] out TDelegate?result) where TDelegate : Delegate { Assembly?assembly = AssemblyFactory.FromExpression( expressionText, className, methodName, returnTypeName, parameterTypeName, parameterName); if (assembly == null) { result = null; return(false); } result = CreateDelegateAndCatchIfThrows <TDelegate>( assembly, returnType, new Type[] { parameterType }, $"Orang.Runtime.{className}", methodName); return(result != null); }
public static bool TryCreateFromAssembly <TDelegate>( string path, Type returnType, Type parameterType, [NotNullWhen(true)] out TDelegate?result) where TDelegate : Delegate { return(TryCreateFromAssembly(path, returnType, new Type[] { parameterType }, out result)); }
/// <summary> /// Removes a subscription from the event. /// </summary> /// <param name="subscription">The delegate to unsubscribe.</param> /// <exception cref="InvalidOperationException">Event was already consumed.</exception> public void Remove(TDelegate?subscription) { if (_ran) { throw new InvalidOperationException("Event was already run."); } _callback = (TDelegate?)Delegate.Remove(_callback, subscription); }
public void AddHandler(TDelegate handler) { lock (_syncLock) { var firstSubscriber = Event == null; Event = (TDelegate)Delegate.Combine(Event, handler); if (firstSubscriber) { _onFirst(); } } }
public void RemoveHandler(TDelegate handler) { lock (_syncLock) { if (Event != null) { Event = (TDelegate?)Delegate.Remove(Event, handler); if (Event == null) { _onLast(); } } } }
/// <summary> /// Consumes the event, preventing future subscription/unsubscription. /// </summary> /// <exception cref="InvalidOperationException">The event was already consumed.</exception> public TDelegate?Consume() { if (_ran) { throw new InvalidOperationException("Event was already run."); } _ran = true; var callback = _callback; _callback = null; return(callback); }
public static bool TryCreateFromSourceText <TDelegate>( string sourceText, Type returnType, Type parameterType, [NotNullWhen(true)] out TDelegate?result) where TDelegate : Delegate { Assembly?assembly = AssemblyFactory.FromSourceText(sourceText); if (assembly == null) { result = null; return(false); } result = CreateDelegateAndCatchIfThrows <TDelegate>(assembly, returnType, new Type[] { parameterType }); return(result != null); }
/// <summary> /// Gets an OpenGL entry point, or null. /// </summary> /// <typeparam name="TDelegate">The delegate type of the entry point.</typeparam> /// <param name="commandName">The name of command associated with the entry point.</param> /// <param name="result">A delegate that can be used to invoke an OpenGL command.</param> /// <returns><c>true</c> if the entry point was found.</returns> /// <exception cref="InvalidOperationException">If the <see cref="CurrentWindow"/> was not set.</exception> /// <exception cref="ObjectDisposedException">If <see cref="Dispose()"/> was called.</exception> public bool TryGetOpenGLEntryPoint <TDelegate>(string commandName, [NotNullWhen(returnValue: true)] out TDelegate?result) where TDelegate : class { CheckIfAlive(); if (currentWindow == null) { throw new InvalidOperationException(Resources.GetMessage(Resources.Key.RenderingContextNotSet)); } var address = nativeRenderingContext !.GetCommandAddress(commandName); if (address == IntPtr.Zero) { result = default; return(false); } result = Marshal.GetDelegateForFunctionPointer <TDelegate>(address); return(true); }
public static bool TryCreateFromCodeFile <TDelegate>( string filePath, Type returnType, Type parameterType, [NotNullWhen(true)] out TDelegate?result) where TDelegate : Delegate { if (!FileSystemHelpers.TryReadAllText(filePath, out string?content, f => WriteError(f))) { result = null; return(false); } Assembly?assembly = AssemblyFactory.FromSourceText(content); if (assembly == null) { result = null; return(false); } result = CreateDelegateAndCatchIfThrows <TDelegate>(assembly, returnType, new Type[] { parameterType }); return(result != null); }
private static bool TryCreateFromAssembly <TDelegate>( string path, Type returnType, Type[] parameters, [NotNullWhen(true)] out TDelegate?result) where TDelegate : Delegate { result = default; if (path == null) { return(false); } int index = path.LastIndexOf(','); if (index <= 0 || index >= path.Length - 1) { WriteError($"Invalid value: {path}. " + "The expected format is \"MyLib.dll,MyNamespace.MyClass.MyMethod\"."); return(false); } string assemblyName = path.Substring(0, index); string methodFullName = path.Substring(index + 1); index = methodFullName.LastIndexOf('.'); if (index < 0 || index >= path.Length - 1) { WriteError($"Invalid method full name: {methodFullName}. " + "The expected format is \"MyNamespace.MyClass.MyMethod\"."); return(false); } Assembly?assembly; try { assembly = Assembly.LoadFrom(assemblyName); } catch (Exception ex) when(ex is ArgumentException || ex is IOException || ex is BadImageFormatException) { WriteError(ex, ex.Message); return(false); } string typeName = methodFullName.Substring(0, index); string methodName = methodFullName.Substring(index + 1); result = CreateDelegateAndCatchIfThrows <TDelegate>(assembly, returnType, parameters, typeName, methodName); return(result != null); }
internal static bool TryCreate <TMethodBase, TDelegate>(this IDelegateFactory <TMethodBase> delegateFactory, object?target, TMethodBase method, out TDelegate? @delegate) where TMethodBase : MethodBase where TDelegate : Delegate { if (delegateFactory == null) { throw new ArgumentNullException(nameof(delegateFactory)); } bool created = delegateFactory.TryCreate(typeof(TDelegate), target, method, out Delegate? d); @delegate = (TDelegate?)d; return(created); }
public static bool TryCreate <TDelegate>(string path, Type[] parameters, out TDelegate?result) where TDelegate : Delegate { result = default; if (path == null) { return(true); } int index = path.LastIndexOf(','); if (index <= 0 || index >= path.Length - 1) { WriteError($"Invalid value: {path}. The expected format is \"MyLib.dll,MyNamespace.MyClass.MyMethod\"."); return(false); } string assemblyName = path.Substring(0, index); string methodFullName = path.Substring(index + 1); index = methodFullName.LastIndexOf('.'); if (index < 0 || index >= path.Length - 1) { WriteError($"Invalid method full name: {methodFullName}. The expected format is \"MyNamespace.MyClass.MyMethod\"."); return(false); } try { Assembly assembly = Assembly.LoadFrom(assemblyName); string typeName = methodFullName.Substring(0, index); Type?type = assembly.GetType(typeName); if (type == null) { WriteError($"Cannot find type '{typeName}' in assembly '{assembly.FullName}'"); return(false); } string methodName = methodFullName.Substring(index + 1); MethodInfo?method = type.GetMethod(methodName, parameters); if (method == null) { WriteError($"Cannot find method '{methodName}' in type '{typeName}'"); return(false); } if (method.IsStatic) { result = (TDelegate)method.CreateDelegate(typeof(TDelegate)); } else { object?typeInstance = Activator.CreateInstance(type); if (typeInstance == null) { WriteError($"Cannot create instance of '{typeName}'"); return(false); } result = (TDelegate)Delegate.CreateDelegate(typeof(TDelegate), typeInstance, methodName); } return(true); } catch (Exception ex) when(ex is ArgumentException || ex is IOException || ex is BadImageFormatException || ex is AmbiguousMatchException || ex is TargetInvocationException || ex is MemberAccessException || ex is MissingMemberException) { WriteError(ex, $"Cannot create delegate: {ex.Message}"); return(false); } }
public static bool TryCreateMatchEvaluator <TDelegate>(string path, out TDelegate?result) where TDelegate : Delegate { return(TryCreate(path, new Type[] { typeof(Match) }, out result)); }