Exemplo n.º 1
0
 public InternalAssemblyRunner(string exePath, Type[] paraTypes, Type returnType, ElevatorOptions options)
 {
     _exePath    = exePath;
     _paraTypes  = paraTypes;
     _returnType = returnType;
     _options    = options;
 }
Exemplo n.º 2
0
        private static InternalAssemblyRunner InternalGenerate(MethodInfo method, object target, Type[] paramaterTypes, Type returnType,
                                                               ElevatorOptions options)
        {
            if (options == null)
            {
                options = new ElevatorOptions();
            }
            if (returnType == null)
            {
                returnType = typeof(void);
            }

            var assyName = options.AssemblyName;
            var assyFile = assyName + ".exe";
            var dir      = Path.Combine(options.TempPath, RandomEx.GetString(8));

            Directory.CreateDirectory(dir);

            var assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(
                new AssemblyName(assyName), AssemblyBuilderAccess.Save, dir);
            var moduleBuilder = assemblyBuilder.DefineDynamicModule(assyName, assyFile);
            var typeBuilder   = moduleBuilder.DefineType("Program", TypeAttributes.Class | TypeAttributes.Public);

            var mainMethod = typeBuilder.DefineMethod("Main",
                                                      MethodAttributes.HideBySig | MethodAttributes.Public | MethodAttributes.Static,
                                                      typeof(int), new Type[] { typeof(string[]) });
            var dynName   = RandomEx.GetString(8);
            var dynMethod = typeBuilder.DefineMethod(dynName,
                                                     MethodAttributes.Public | MethodAttributes.Static,
                                                     returnType, paramaterTypes);

            var ref1 = EmitModifiedMethod(dynMethod, method, target);
            var ref2 = EmitMainMethod(mainMethod, dynName, paramaterTypes);

            typeBuilder.CreateType();
            assemblyBuilder.SetEntryPoint(mainMethod, PEFileKinds.ConsoleApplication);
            assemblyBuilder.Save(assyFile);

            var references = assemblyBuilder.GetReferencedAssemblies()
                             .Select(r => Assembly.ReflectionOnlyLoad(r.FullName))
                             .Concat(ref1)
                             .Concat(ref2)
                             .Concat(options.Dependancies)
                             .Distinct(new AssemblyFullNameEqualityComparer())
                             .Where(a => !a.IsInGAC());

            foreach (var refAssy in references)
            {
                var refName = Path.GetFileName(refAssy.Location);
                File.Copy(refAssy.Location, Path.Combine(dir, refName));
            }

            return(new InternalAssemblyRunner(Path.Combine(dir, assyFile), paramaterTypes, returnType, options));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Compiles the provided delegate into a separate assembly, so that the code can be executed with different
        /// paramaters than the current process. (run as administrator, etc)
        /// </summary>
        /// <param name="method">The delegate to compile</param>
        /// <param name="options">Optionally specify custom compilation options</param>
        /// <returns>Returns replacement delegate that will execute the original delegate but in a different process.</returns>
        public static ElevatorAssembly <Action <T1, T2, T3, T4> > Compile <T1, T2, T3, T4>(Action <T1, T2, T3, T4> method, ElevatorOptions options = null)
        {
            var assy = InternalGenerate(method.Method, method.Target, new[] { typeof(T1), typeof(T2), typeof(T3), typeof(T4) }, null, options);

            return(new ElevatorAssembly <Action <T1, T2, T3, T4> >((one, two, three, four) => assy.Run(new object[] { one, two, three, four }), assy.Dispose));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Compiles the provided delegate into a separate assembly, so that the code can be executed with different
        /// paramaters than the current process. (run as administrator, etc)
        /// </summary>
        /// <param name="method">The delegate to compile</param>
        /// <param name="options">Optionally specify custom compilation options</param>
        /// <returns>Returns replacement delegate that will execute the original delegate but in a different process.</returns>
        public static ElevatorAssembly <Action <T> > Compile <T>(Action <T> method, ElevatorOptions options = null)
        {
            var assy = InternalGenerate(method.Method, method.Target, new[] { typeof(T) }, null, options);

            return(new ElevatorAssembly <Action <T> >((one) => assy.Run(new object[] { one }), assy.Dispose));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Compiles the provided delegate into a separate assembly, so that the code can be executed with different
        /// paramaters than the current process. (run as administrator, etc)
        /// </summary>
        /// <param name="method">The delegate to compile</param>
        /// <param name="options">Optionally specify custom compilation options</param>
        /// <returns>Returns replacement delegate that will execute the original delegate but in a different process.</returns>
        public static ElevatorAssembly <Action> Compile(Action method, ElevatorOptions options = null)
        {
            var assy = InternalGenerate(method.Method, method.Target, new Type[] { }, null, options);

            return(new ElevatorAssembly <Action>(() => assy.Run(new object[] { }), assy.Dispose));
        }
Exemplo n.º 6
0
        /// <summary>
        /// Compiles the provided delegate into a separate assembly, so that the code can be executed with different
        /// paramaters than the current process. (run as administrator, etc)
        /// </summary>
        /// <param name="method">The delegate to compile</param>
        /// <param name="options">Optionally specify custom compilation options</param>
        /// <returns>Returns replacement delegate that will execute the original delegate but in a different process.</returns>
        public static ElevatorAssembly <Func <T1, T2, T3, T4, T5, T6, T7, TResult> > Compile <T1, T2, T3, T4, T5, T6, T7, TResult>(Func <T1, T2, T3, T4, T5, T6, T7, TResult> method, ElevatorOptions options = null)
        {
            var assy = InternalGenerate(method.Method, method.Target, new Type[] { typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7) }, typeof(TResult), options);

            return(new ElevatorAssembly <Func <T1, T2, T3, T4, T5, T6, T7, TResult> >((one, two, three, four, five, six, seven) => (TResult)assy.Run(new object[] { one, two, three, four, five, six, seven }), assy.Dispose));
        }
Exemplo n.º 7
0
        /// <summary>
        /// Compiles the provided delegate into a separate assembly, so that the code can be executed with different
        /// paramaters than the current process. (run as administrator, etc)
        /// </summary>
        /// <param name="method">The delegate to compile</param>
        /// <param name="options">Optionally specify custom compilation options</param>
        /// <returns>Returns replacement delegate that will execute the original delegate but in a different process.</returns>
        public static ElevatorAssembly <Func <T1, T2, TResult> > Compile <T1, T2, TResult>(Func <T1, T2, TResult> method, ElevatorOptions options = null)
        {
            var assy = InternalGenerate(method.Method, method.Target, new Type[] { typeof(T1), typeof(T2) }, typeof(TResult), options);

            return(new ElevatorAssembly <Func <T1, T2, TResult> >((one, two) => (TResult)assy.Run(new object[] { one, two }), assy.Dispose));
        }
Exemplo n.º 8
0
        /// <summary>
        /// Compiles the provided delegate into a separate assembly, so that the code can be executed with different
        /// paramaters than the current process. (run as administrator, etc)
        /// </summary>
        /// <param name="method">The delegate to compile</param>
        /// <param name="options">Optionally specify custom compilation options</param>
        /// <returns>Returns replacement delegate that will execute the original delegate but in a different process.</returns>
        public static ElevatorAssembly <Action <T1, T2, T3, T4, T5, T6, T7, T8> > Compile <T1, T2, T3, T4, T5, T6, T7, T8>(Action <T1, T2, T3, T4, T5, T6, T7, T8> method, ElevatorOptions options = null)
        {
            var assy = InternalGenerate(method.Method, method.Target, new[] { typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8) }, null, options);

            return(new ElevatorAssembly <Action <T1, T2, T3, T4, T5, T6, T7, T8> >((one, two, three, four, five, six, seven, eight) => assy.Run(new object[] { one, two, three, four, five, six, seven, eight }), assy.Dispose));
        }