I'm using CompilerContainer on static method in static class and have StackOverflowException.
Have this static class with 1 static method:
public static class StringUtility
{
public static bool StringEqual(string str1, string str2)
{
return (string.Compare(str1, str2, true) == 0);
}
}
and defined CompilerContainer(using linq expressions):
[CompilerContainer(typeof(Expression))]
public static class LinqExtensions
{
[Compiler(typeof(StringUtility), "StringEqual", TargetKind.Method | TargetKind.Static)]
public static Expression StringEqual(Expression string1Expression, Expression string2Expression)
{
// sourceExpression -> Identity
Expression<Func<string, string, bool>> expression = (string1, string2) => StringUtility.StringEqual(string1, string2);
return expression.BindParameters(string1Expression, string2Expression);
}
}
And when i use this in linq query like this:
Query.All<Country>().Where(country => StringUtility.StringEqual(country.Code2, "uk"))
Then it will stops on never-ending recursion on calling LinqExtensions.StringEqual method and throws StackOverflowException.
I know that this never-ending recursion is caused by
=> StringUtility.StringEqual(string1, string2) defined in expression, but i dont know how to solve such problem.
Can you fix this CompilerContainer to work out?
Updated at 02.03.2010 8:16:36
Your sample will work for me, but i want to explain how to use CompilerContainer more generally.
What i have is static class called StringUtility which has more than this one StringEqual method, more complex, like this:
public static string RemoveDiacritics(string text)
{
// complex code here
...
}
and i want to use this method (and other complex methods defined in StringUtility) in linq queries like:
var noteWithoutDiacritics = Query.All<PlayerNote>().Where(...).Select(note => StringUtility.RemoveDiacritics(note.Description));
I have about 10 methods (can be more in future) in class StringUtility and i want to define CompilerContainer methods to use those methods from StringUtility, but i dont want to rewrite each StringUtility method body into LinqExtensions (CompilerContainer) class mapped methods. This is what i want. Is this possible or not?
Updated at 02.03.2010 19:33:26
If you don't want to maintain two versions of the same code, there is a good option:
[CompilerContainer(typeof(Expression))]
pubic static class MathUtils
{
private static Expression<func<...>> addExpression = (a,b) => a + b; // The only instance of this code
private static Func<...> addDelegate = addExpression.Compile();
public static bool Add(int a, int b)
{
return addDelegate.Invoke(a,b);
}
[Compiler(typeof(MathUtils), "Add", TargetKind.Method | TargetKind.Static)]
public static Expression AddCompiler(Expression a, Expression b)
{
return addExpression.BindParameters(a, b);
}
}
Thanks for hint, i will try it, this will look good for what i want.
This thread was imported from our support forum. The original discussion may contain more detailed answer.
asked
Mar 02 '10 at 07:37
Peter Šulek
492●31●32●36