首页 文章

如何通过命令提示符编译C#应用程序

提问于
浏览
0

我有一个文件夹 . 在那我有三个cs文件 .

Demo.cs

using System;

namespace Demo
{
    public class Test
    {
        public static Entity entity = new Entity();

        public static void Main(string[] args)
        {
            var objectHandler = Activator.CreateInstance(null,
                                                         args);
            var obj = objectHandler.Unwrap();
            entity.GetAnnotation(obj.GetType());
        }
    }
}

Entity.cs

using System;

namespace Demo
{
    public class Entity
    {
        public void GetAnnotation(Type classname)
        {
            Attribute[] dataAnnotationlist = Attribute.GetCustomAttributes(propInfo);
            foreach (var dataannotationAttribute in dataAnnotationlist)
            {
                //some operation to get annotation property from Employee.cs class
            }
        }
    }
}

Employee.cs

using System.ComponentModel.DataAnnotations;

namespace Demo
{
    public class Employee
    {
        [Display(Name = "name")]
        public string name { get; set; }
    }
}

我使用反射从类文件(Employee.cs)创建了XML文件格式 . 但尝试运行命令提示符时发生错误 . 它在视觉工作室运行 .

我想使用命令提示符运行Test.cs,Entity.cs,并将“Employee.cs”作为字符串参数传递给Main方法 . 现在,我已通过硬编码,

System.Runtime.Remoting.ObjectHandle objectHandler = Activator.CreateInstance(null, "Demo.Employee");

它的工作正常,但如何通过命令传递它 .

发生错误的是:

Entity.cs(8,29):错误CS0234:名称空间'System.ComponentModel'中不存在类型或命名空间名称'DataAnnotations'(您是否缺少程序集引用?)Entity.cs(9,19):错误CS0234:命名空间'System.Data'中不存在类型或命名空间名称'Objects'(您是否缺少程序集引用?)Employee.cs(6,33):error CS0234:类型或命名空间名称'DataAnnotations '在命名空间'System.ComponentModel'中不存在(你是否缺少程序集引用?)

它还显示“DataAnnotations”和“Objects”的错误 .

我怎么解决这个问题?

1 回答

  • 0

    一个选项就是build .csproj with MSBUILD .

    更有趣的是通过csc的命令行参数自己配置所有依赖项 . 对于您的即时错误,您需要使用类似于以下的 /r: 命令添加引用

    csc /out:Test.exe /r:System.ComponentModel.DataAnnotations.dll *.cs
    

    有关csc命令行参数的更多详细信息,请检查帮助 csc /? 或MSDN CSC command line optionsBuilding with CSC .

相关问题