C# Error
CS0765 – Partial methods with only a defining declaration or removed conditional methods cannot be used in expression trees
Reason for the Error & Solution
Partial methods with only a defining declaration or removed conditional methods cannot be used in expression trees
Although a call to a removed partial method is an expression, it is not an acceptable expression in an expression tree.
To correct this error
- Add an implementing declaration for the partial method, or remove the code that is causing the conditional method to be excluded from compilation.
Example
The following code generates CS0765 in two locations:
// cs0765.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
public delegate void dele();
public class ConClass
{
[Conditional("CONDITION")]
public static void TestMethod() { }
}
public partial class PartClass : IEnumerable
{
List<object> list = new List<object>();
partial void Add(int x);
public IEnumerator GetEnumerator()
{
for (int i = 0; i < list.Count; i++)
yield return list[i];
}
static void Main()
{
Expression<Func<PartClass>> testExpr1 = () => new PartClass { 1, 2 }; // CS0765
Expression<dele> testExpr2 = () => ConClass.TestMethod(); // CS0765
}
}