public static dynamic Compile(CotMethod cotMethod, CancellationToken token = default) { var rndClass = CodeSnippet.RandomString(); var pageOfCode = CodeSnippet.GeneratePage(rndClass, new[] { cotMethod.ToString() }, out var refs); var errors = Roslyn.Compile(pageOfCode, refs, out var asm, token); if (errors.Length == 0) { var t = asm.GetType(rndClass); try { var mn = cotMethod.Info.Name.ValueText; var d = RuntimeDelegateFactory.StaticMethod(t, mn); return(d); } catch (ArgumentException) { throw; } } throw new CompilationException(errors); }
public static DelegateWrapper <T> Compile <T>(CotMethod cotMethod, CancellationToken token = default) where T : Delegate { var rndClass = CodeSnippet.RandomString(); var pageOfCode = CodeSnippet.GeneratePage(rndClass, new[] { cotMethod.ToString() }, out var refs); var errors = Roslyn.Compile(pageOfCode, refs, out var asm, token); if (errors.Length == 0) { var t = asm.GetType(rndClass); try { var mn = cotMethod.Info.Name.ValueText; var d = Delegate.CreateDelegate(typeof(T), t, mn, false, true) as T; return(new DelegateWrapper <T>(d, mn)); } catch (ArgumentException) { throw; } } throw new CompilationException(errors); }