Exemple #1
0
        /// <summary>
        /// Generates the class en compiles it into a producer.
        /// </summary>
        /// <param name="code">The code.</param>
        /// <returns>The producer.</returns>
        public IProducer CreateProducer(string code)
        {
            var args = new CompilerInstructions();

            args.ClassName = "_" + Guid.NewGuid().ToString("N");
            args.Code      = @"<<USINGS>>

	public class <<CLASS_NAME>>: <<BASE_TYPE>>
	{
		public <<MODIFIER>> object Run()
		{
			<<CODE>>;
	
			return null;
		}
	}"    ;
            args.Code      = args.Code.Replace("<<CLASS_NAME>>", args.ClassName);
            args.Code      = args.Code.Replace("<<BASE_TYPE>>", FixFullName(_producerType));
            args.Code      = args.Code.Replace("<<CODE>>", code);
            args.AssemblyLocations.AddRange(this.AssemblyLocations);

            if (Usings.Count > 0)
            {
                string usings = "using " + String.Join(";\nusing ", Usings) + ";";
                args.Code = args.Code.Replace("<<USINGS>>", usings);
            }
            else
            {
                args.Code = args.Code.Replace("<<USINGS>>", "");
            }

            if (_producerType.IsClass)
            {
                args.Code = args.Code.Replace("<<MODIFIER>>", "override");
            }
            else
            {
                args.Code = args.Code.Replace("<<MODIFIER>>", "");
            }

            return(_compiler.CompileAndCreateObject <IProducer>(args));
        }
Exemple #2
0
 /// <summary>
 /// Compiles the and create object.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="compiler">The compiler.</param>
 /// <param name="instructions">The instructions.</param>
 /// <param name="constructorParameters">The constructor parameters.</param>
 /// <returns></returns>
 public static T CompileAndCreateObject <T>(this ICompiler compiler, ICompilerInstructions instructions, params object[] constructorParameters)
 {
     return((T)compiler.CompileAndCreateObject(instructions, constructorParameters));
 }