/// <summary> /// Initializes the specified options. /// </summary> /// <param name="options">The options.</param> private void Initialize(DynamicStaticMethodOptions options) { try { options.CheckNullObject(nameof(options)); options.DynamicCode.CheckEmptyString(nameof(options.DynamicCode)); Type type = null; if (!string.IsNullOrWhiteSpace(options.ClassName) && !string.IsNullOrWhiteSpace(options.MethodName)) { if (options.ClassName == options.MethodName) { throw new InvalidObjectException(nameof(options), data: new { options }, reason: "Conflict name for compile closure."); } type = ReflectionExtension.SmartGetType(string.Format("{0}.{1}", _namespace, options.ClassName)); if (type != null) { _methodInfo = type.GetMethod(options.MethodName); if (_methodInfo != null && _methodInfo.IsStatic) { return; } } } var tmpId = Guid.NewGuid().ToString("N"); var generatedCode = BuildMethodCodeAsClass(options, tmpId); TempAssemblyProvider provider = new TempAssemblyProvider(); var tempAssembly = provider.CreateTempAssembly(generatedCode.AsArray()); type = ReflectionExtension.SmartGetType(string.Format("{0}.DynamicStatiClass{1}", _namespace, tmpId)); type.CheckNullObjectAsInvalid(nameof(options), data: new { options, generatedCode, tmpId }); _methodInfo = type.GetMethod(string.Format("DynamicStaticMethod{0}", tmpId)); } catch (Exception ex) { throw ex.Handle(new { options }); } }
/// <summary> /// Builds the method code as class. /// </summary> /// <param name="options">The options.</param> /// <param name="tmpId">The temporary identifier.</param> /// <returns></returns> private static string BuildMethodCodeAsClass(DynamicStaticMethodOptions options, string tmpId) { const string fullFormat = @" using System; using Beyova; namespace {0}{{ public static class DynamicStatiClass{1} {{ public static {2} DynamicStaticMethod{1}() {{ {3} }} }} }} "; return(string.Format(fullFormat, _namespace, tmpId, (options.ReturnObjectType ?? typeof(void)).ToCodeLook(), options.DynamicCode)); }
/// <summary> /// Initializes a new instance of the <see cref="DynamicStaticMethod"/> class. /// </summary> /// <param name="options">The options.</param> internal DynamicStaticMethod(DynamicStaticMethodOptions options) { Options = options; Initialize(options); }