错误 CS1061 不包含定义 - Roslyn 编译问题
error CS1061 does not contain a definition - Roslyn compilation issue
我正在使用 Roslyn 在具有定义
的业务对象(比方说,Customer
)上生成 Linq 表达式
public class Customer
{
public List<Order> Orders { get; set; }
}
要编译 C# code
的 Roslyn 代码是 -
var linqType = typeof(System.Linq.Enumerable);
List<MetadataReference> metadataReferences = new List<MetadataReference>()
{
//Other business DLLs as well
MetadataReference.CreateFromFile(linqType.Assembly.Location)
};
//mscorlib
car dlls = AppContext.GetData("TRUSTED_PLATFORM_ASSEMBLIES").ToString().Split(new char[] { ';' });
foreach (var platformDLL in dlls)
{
metadataReferences.Add(MetadataReference.CreateFromFile(platformDLL));
}
// Removed: Get Units
var compilationOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary,
optimizationLevel: OptimizationLevel.Debug,
assemblyIdentityComparer: DesktopAssemblyIdentityComparer.Default,
allowUnsafe: true);
compilationOptions.WithUsings(GetNamespaces());
var compilation = CSharpCompilation.Create(assemblyName: "customer",
syntaxTrees: units.Select(x => x.SyntaxTree).ToArray(),
references: metadataReferences,
options: compilationOptions);
生成的Linq表达式如下。
Exists<Customer>(f => f.Orders.Any() == true)
class还有以下使用
using System;
using System.Text;
using System.Linq;
但是,我仍然遇到以下错误
error CS1061: 'Customer' does not contain a definition for 'Orders.Any()' and no extension method 'Orders.Any()' accepting a first argument of type 'Customer' could be found (are you missing a using directive or an assembly reference?)
代码生成正确,但使用 Roslyn 编译代码失败并出现上述错误。当我将生成的代码复制粘贴到 Visual Studio 时,编译没有错误
听起来您创建了一个 MemberAccessExpression
,成员名称为 Orders.Any()
。
MemberAccessExpression 只能使用单个成员;该链需要两个嵌套表达式。
我正在使用 Roslyn 在具有定义
的业务对象(比方说,Customer
)上生成 Linq 表达式
public class Customer
{
public List<Order> Orders { get; set; }
}
要编译 C# code
的 Roslyn 代码是 -
var linqType = typeof(System.Linq.Enumerable);
List<MetadataReference> metadataReferences = new List<MetadataReference>()
{
//Other business DLLs as well
MetadataReference.CreateFromFile(linqType.Assembly.Location)
};
//mscorlib
car dlls = AppContext.GetData("TRUSTED_PLATFORM_ASSEMBLIES").ToString().Split(new char[] { ';' });
foreach (var platformDLL in dlls)
{
metadataReferences.Add(MetadataReference.CreateFromFile(platformDLL));
}
// Removed: Get Units
var compilationOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary,
optimizationLevel: OptimizationLevel.Debug,
assemblyIdentityComparer: DesktopAssemblyIdentityComparer.Default,
allowUnsafe: true);
compilationOptions.WithUsings(GetNamespaces());
var compilation = CSharpCompilation.Create(assemblyName: "customer",
syntaxTrees: units.Select(x => x.SyntaxTree).ToArray(),
references: metadataReferences,
options: compilationOptions);
生成的Linq表达式如下。
Exists<Customer>(f => f.Orders.Any() == true)
class还有以下使用
using System;
using System.Text;
using System.Linq;
但是,我仍然遇到以下错误
error CS1061: 'Customer' does not contain a definition for 'Orders.Any()' and no extension method 'Orders.Any()' accepting a first argument of type 'Customer' could be found (are you missing a using directive or an assembly reference?)
代码生成正确,但使用 Roslyn 编译代码失败并出现上述错误。当我将生成的代码复制粘贴到 Visual Studio 时,编译没有错误
听起来您创建了一个 MemberAccessExpression
,成员名称为 Orders.Any()
。
MemberAccessExpression 只能使用单个成员;该链需要两个嵌套表达式。