Ejemplo n.º 1
0
		/// <summary>
		/// Compile source code
		/// </summary>
		/// <param name="code">full source code to compile</param>
		/// <param name="references">assembly references</param>
		/// <param name="language">target language</param>
		/// <param name="profile">compiler profile</param>
		public void Compile(string code, string[] references, SupportedLanguage language, CompilerProfile profile)
		{
			var properties = new Dictionary<string, string> {{CompilerVersion, profile.CompilerVersion}};
			CodeDomProvider provider;

			switch (language)
			{
				case SupportedLanguage.CSharp:
					provider = new CSharpCodeProvider(properties);
					break;
				case SupportedLanguage.VisualBasic:
					provider = new VBCodeProvider(properties);
					break;
				default:
					throw new ArgumentException();
			}

			var parameters = new CompilerParameters
			{
				GenerateExecutable = false,
				GenerateInMemory = false,
				IncludeDebugInformation = false
			};

			parameters.ReferencedAssemblies.AddRange(references);

			var CompilerOptions = new List<string>();
			if (language == SupportedLanguage.CSharp)
				CompilerOptions.Add("/unsafe");

			if (profile.NoStdLib)
				CompilerOptions.Add("/nostdlib");

			if (CompilerOptions.Count > 0)
				parameters.CompilerOptions = string.Join(" ", CompilerOptions.ToArray());

			var results = provider.CompileAssemblyFromSource(parameters, code);
			AssemblyLocation = null;
			Errors = results.Errors;

			if (!results.Errors.HasErrors)
				AssemblyLocation = results.CompiledAssembly.Location;
		}
Ejemplo n.º 2
0
		public sealed override string[] GetReferences(bool keepextension, CompilerProfile profile)
		{
			var references = new List<string>();
			var resolver = new ReflexilAssemblyResolver();

			var filename = _mdefsource.DeclaringType.Module.Image.FileName;
			var currentPath = Path.GetDirectoryName(filename);

			if (currentPath != null)
			{
				Directory.SetCurrentDirectory(currentPath);
				resolver.AddSearchDirectory(currentPath);
				// Properly register assembly, so we can find it even if the name is changed
				resolver.RegisterAssembly(_mdefsource.DeclaringType.Module.Assembly);
			}

			foreach (var asmref in CompileReferences)
			{
				string reference;

				if ((profile == null || !profile.NoStdLib) && (asmref.Name == "mscorlib" || asmref.Name.StartsWith("System")))
				{
					reference = asmref.Name + (keepextension ? ".dll" : string.Empty);
				}
				else
				{
					try
					{
						var asmdef = resolver.Resolve(asmref);
						reference = asmdef.MainModule.Image.FileName;
					}
					catch (Exception)
					{
						continue;
					}
				}

				if (!references.Contains(reference))
					references.Add(reference);
			}

			return references.ToArray();
		}
Ejemplo n.º 3
0
		public virtual String[] GetReferences(bool keepextension, CompilerProfile profile)
		{
			// We cannot use abstract modifier because of the designer, let's derived class handle this method
			throw new NotImplementedException();
		}