Reputation: 27953
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 ?
Upvotes: 0
Views: 2445
Reputation: 1569
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);
Upvotes: 0
Reputation: 44448
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 roslyn 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
Upvotes: 4