Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I'm trying to develop a code compiler using C# but the problem is that I'm getting These Errors.
Error: c:\Users\ss\AppData\Local\Temp\xxpatmuh.0.cs(3,14) : error CS0234: The type or namespace name 'ComponentModel' does not exist in the namespace 'System' (are you missing an assembly reference?)<br />
Line:3<br />
Error: c:\Users\ss\AppData\Local\Temp\xxpatmuh.0.cs(6,14) : error CS0234: The type or namespace name 'Management' does not exist in the namespace 'System' (are you missing an assembly reference?)<br />
Line:6<br />
Error: c:\Users\ss\AppData\Local\Temp\xxpatmuh.0.cs(7,14) : error CS0234: The type or namespace name 'Net' does not exist in the namespace 'System' (are you missing an assembly reference?)<br />
Line:7<br />
Error: c:\Users\ss\AppData\Local\Temp\xxpatmuh.0.cs(8,14) : error CS0234: The type or namespace name 'Net' does not exist in the namespace 'System' (are you missing an assembly reference?)<br />
Line:8<br />
Error: c:\Users\ss\AppData\Local\Temp\xxpatmuh.0.cs(127,69) : error CS0246: The type or namespace name 'AsyncCompletedEventArgs' could not be found (are you missing a using directive or an assembly reference?)<br />
Line:127


PS: This is what I have in the reference:-
Screenshot

What I have tried:

Source.cs
C#
namespace Builder
{
    using Microsoft.CSharp;
    using System.CodeDom.Compiler;
    using System.Collections.Generic;
    using System.IO;
    using System.Windows.Forms;

    public class Source
    {
        public static void Editor(string title, string filename, string message)
        {
            string SourceCode = Properties.Resources.Stub;
            SourceCode = SourceCode.Replace("[Title]", title);
            SourceCode = SourceCode.Replace("%Path%", filename);
            SourceCode = SourceCode.Replace("%Contents%", message);

            var ProviderOptions = new Dictionary<string, string> { { "CompileVersion", "v.4.7" } };

            using (var provider = new CSharpCodeProvider())
            {
                var Params = new CompilerParameters
                {
                    OutputAssembly = "Build.exe",
                    GenerateExecutable = true,
                    CompilerOptions = "/target:exe"
                };
                CompilerResults results = provider.CompileAssemblyFromSource(Params, SourceCode);
                if (!results.Errors.HasErrors)
                {
                    MessageBox.Show("Created", "rSuccessrr", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    foreach (CompilerError compilerError in results.Errors)
                    {
                        File.AppendAllText("Error.txt", $"Error: {compilerError.ToString()}\r\nLine:{compilerError.Line}\r\n");
                    }
                }
            }
        }
    }
}


Stub.txt

C#
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Management;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Threading;
using System.Reflection;

namespace Stub
{

    internal static class Program
{
    public static void Main(){

}
}
Posted
Updated 12-Jul-21 22:11pm

CompilerParameters.ReferencedAssemblies Property (System.CodeDom.Compiler) | Microsoft Docs[^]

Just like your C# project the code compiler needs to be told which assemblies it needs to reference. Just add the missing assemblies to this collection before you try and compile the code. If you want to ensure that all assemblies are included you can add them all:
C#
Params
  .ReferencedAssemblies
  .AddRange(
    Assembly
      .GetExecutingAssembly()
      .GetReferencedAssemblies()
      .Select(asm => $"{asm.Name}.dll")
      .ToArray()
  );
 
Share this answer
 
Comments
MohammedZr 13-Jul-21 12:25pm    
Thanks, I have added the Assemblies but I'm getting another error

Error: c:\Users\ss\AppData\Local\Temp\gfw0yh3f.0.cs(26,20) : error CS1070: The type name 'Socket' could not be found. This type has been forwarded to assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. Consider adding a reference to that assembly.
Line:26
Right click your Project in the Solutions Pane and select "Add Reference".
Locate System.ComponentModel in the Framework list and add it to the project.
 
Share this answer
 
Comments
MohammedZr 13-Jul-21 0:38am    
The Same Problem appears

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900