0

I'm creating dynamic converter which converts Visual FoxPro expression to C# expressions which are used in Razor views.

FoxPro has contains operator $

a $ b

returns true if a if substring of b like Contains(a,b) :

public static bool Contains(string a, string b)
{
    if (a == null || b == null)
        return false;
    return b.Contains(a);
}

Replacing $ operator with this Contains method call requires creating sophisticated parser. Not sure how this parser can implemented. a$b can be part of expression, a and b can be string expressions.

How to make it work ? Is is possible to implement contains operator ( operator name can changed, parser can emit it easily) ? Or is there some other way to avoid manual conversion of FoxPro expressions ?

Expressions are used in C# Razor Views in ASP.NET MVC4 application.

Update

I can use standard operator, for example % . How to implement a % b operator in Razor views for strings which returns true if a contains in b ?

Andrus
  • 26,339
  • 60
  • 204
  • 378
  • http://stackoverflow.com/questions/172658/operator-overloading-with-c-sharp-extension-methods has some background on why you can't even overload an existing operator to do what you want. – ClickRick May 24 '14 at 20:40
  • If the answers below don't help, go back to working out how to implement the parser, as you're in danger of falling victim to [the XY problem](http://mywiki.wooledge.org/XyProblem). – ClickRick May 24 '14 at 20:53
  • I can use string class which inherits from standard string class. Comment in answer wrote that it is poosible to implement containedin operator in this case. How to implement it? – Andrus May 25 '14 at 15:15

2 Answers2

4

Operator overloading is only possible with known operators: +, *, ==, etc. $ is not a known operator so it is not possible what you want to do.

You can use the new platform to change the language itself and make it possible (see the presentation at Build 2014 where they demonstrate how to use fancy accents to denote a string instead of ").

In textform (see: "Example of updating the compiler"): MSDN Blogs: Taking a tour of Roslyn

Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
  • I can use standard operator, for example `%` . How to implement `a % b` operator for strings wchis returns true if a contains in b ? I updated question – Andrus May 24 '14 at 20:42
  • Without rewriting part of the language, you can't. http://stackoverflow.com/questions/172658/operator-overloading-with-c-sharp-extension-methods explains why. – ClickRick May 24 '14 at 20:46
  • Application should run in .NET 4 with ASP.NET MVC4 and Razor 2. I'm using open-source RazorEngine to run those converteted expressions inside razor views. How to implement and use $ operator in RazorEngine, force Razorengine to use new compiler ? – Andrus May 24 '14 at 21:11
  • You cannot overload the operators of the `string` class since you don't define it nor can you inherit from it. – Jeroen Vannevel May 24 '14 at 21:29
  • Expressions use `+` operator and known string methods. I can create new class and implement desired methods and hopefully `+` operator. How to implement `a containedin b` or `a%b` operator for this string-like class ? – Andrus May 25 '14 at 17:12
0

What you can do is use an extension method to simplify the job of your "parsing" tool.

If you put a method such as this:

public static bool FPContains(this string a, string b)
{
    if (a == null || b == null)
        return false;
    return b.Contains(a);
}

in a static class (that's an important constraint), then you can use your new FPContains method as though it were directly implemented in the string type, such as:

bool isContained = string1.FPContains(string2);
ClickRick
  • 1,553
  • 2
  • 17
  • 37
  • 1
    You will never be able to call that as an extension method since its signature matches an instance method. – Mike Zboray May 24 '14 at 20:53
  • Yes, FoxPro parser can emit `.FPContains(` string to Razor view if in encounters `$` . Difficult part is how to find end of expression where to emit closing `)` ? How to implement `)` emitting ? Also string1 can be expression. So it needs to wapped into () braces to use extension method. How to find start of string1 expression to emit `(` before it ? – Andrus May 24 '14 at 20:57