从文件编译程序集时找不到命名空间错误
Namespace not found error when compiling assembly from files
我想要实现的是从我生成的 c# 类 动态生成一个项目。
此类'内容与实体framework.The的代码优先代码生成的内容类似,如下所示:
namespace ElasticTables
{
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations.KeyAttribute;
[Table("address")]
public partial class address
{
[Key]
public decimal id { get; set; }
public string name { get; set; }
}
}
我从我的数据库中的表生成这个文件,然后尝试以编程方式编译它,以便我可以在另一个使用 API.
的项目中引用生成的项目
编译时的主要错误是:
The type or namespace name 'KeyAttribute' does not exist in the namespace 'System.ComponentModel.DataAnnotations' (are you missing an assembly reference?)
The type or namespace 'Key' could not be found
The type or namespace 'Table' could not be found.
我正在使用 'CSharpCodeProvider'
var provider = new CSharpCodeProvider();
var options = new CompilerParameters
{
OutputAssembly = "ElasticTables.dll",
CompilerOptions = "/optimize"
};
我有以下引用的程序集
options.ReferencedAssemblies.Add(Directory.GetCurrentDirectory() + "\EntityFramework.dll");
options.ReferencedAssemblies.Add(Directory.GetCurrentDirectory() + "\EntityFramework.SqlServer.dll");
我有一个字符串数组,其中包含名为 sources 的文件路径,我尝试使用以下行进行编译
CompilerResults results = provider.CompileAssemblyFromFile(options, sources);
非常感谢您的帮助。
您需要引用所有需要的程序集(如错误所述),因此您需要添加,我至少要说:
options.ReferencedAssemblies.Add("System.dll");
options.ReferencedAssemblies.Add("System.ComponentModel.DataAnnotations.dll");
可能还需要其他人
关于你问题中的评论,是的,你应该指定options.OutputAssembly
此外,在您生成的代码中:
using System.ComponentModel.DataAnnotations.KeyAttribute;
KeyAttribute
不是命名空间,因此编译时可能会出错。
我还会使用 usings
before 命名空间。这不是严格需要的,也不是错误,但这是常见的做法(这样你就可以确定引用的程序集来自 global
命名空间,而不是你的 class 位于 [以防出现名称冲突])
您是否尝试过添加对 "System.dll" 和 "System.ComponentModel.DataAnnotations.dll" 的引用(对于 System.ComponentModel 内容)?
(因为您可能确实缺少程序集参考)
options.ReferencedAssemblies.Add(
Path.Combine(
Directory.GetCurrentDirectory(),
"System.ComponentModel.DataAnnotations.dll"));
我想要实现的是从我生成的 c# 类 动态生成一个项目。 此类'内容与实体framework.The的代码优先代码生成的内容类似,如下所示:
namespace ElasticTables
{
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations.KeyAttribute;
[Table("address")]
public partial class address
{
[Key]
public decimal id { get; set; }
public string name { get; set; }
}
}
我从我的数据库中的表生成这个文件,然后尝试以编程方式编译它,以便我可以在另一个使用 API.
的项目中引用生成的项目编译时的主要错误是:
The type or namespace name 'KeyAttribute' does not exist in the namespace 'System.ComponentModel.DataAnnotations' (are you missing an assembly reference?)
The type or namespace 'Key' could not be found
The type or namespace 'Table' could not be found.
我正在使用 'CSharpCodeProvider'
var provider = new CSharpCodeProvider();
var options = new CompilerParameters
{
OutputAssembly = "ElasticTables.dll",
CompilerOptions = "/optimize"
};
我有以下引用的程序集
options.ReferencedAssemblies.Add(Directory.GetCurrentDirectory() + "\EntityFramework.dll");
options.ReferencedAssemblies.Add(Directory.GetCurrentDirectory() + "\EntityFramework.SqlServer.dll");
我有一个字符串数组,其中包含名为 sources 的文件路径,我尝试使用以下行进行编译
CompilerResults results = provider.CompileAssemblyFromFile(options, sources);
非常感谢您的帮助。
您需要引用所有需要的程序集(如错误所述),因此您需要添加,我至少要说:
options.ReferencedAssemblies.Add("System.dll");
options.ReferencedAssemblies.Add("System.ComponentModel.DataAnnotations.dll");
可能还需要其他人
关于你问题中的评论,是的,你应该指定options.OutputAssembly
此外,在您生成的代码中:
using System.ComponentModel.DataAnnotations.KeyAttribute;
KeyAttribute
不是命名空间,因此编译时可能会出错。
我还会使用 usings
before 命名空间。这不是严格需要的,也不是错误,但这是常见的做法(这样你就可以确定引用的程序集来自 global
命名空间,而不是你的 class 位于 [以防出现名称冲突])
您是否尝试过添加对 "System.dll" 和 "System.ComponentModel.DataAnnotations.dll" 的引用(对于 System.ComponentModel 内容)? (因为您可能确实缺少程序集参考)
options.ReferencedAssemblies.Add(
Path.Combine(
Directory.GetCurrentDirectory(),
"System.ComponentModel.DataAnnotations.dll"));