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

时间:2014-09-03 05:36:00

标签: c#

我有一个文件夹。我有三个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:类型或命名空间名称           ' DataAnnotations'在命名空间中不存在           ' System.ComponentModel' (您是否缺少程序集引用?)Entity.cs(9,19):error CS0234:类型或命名空间名称   '对象'           名称空间中不存在System.Data' (你错过了吗?           程序集引用?)Employee.cs(6,33):错误CS0234:类型或命名空间名称' DataAnnotations'不           名称空间中不存在System.ComponentModel' (你错过了吗?           装配参考?)

它还显示" DataAnnotations"的错误和"对象"。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分: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

相关问题