Execute a user-provided string containing c# code [duplicate] - c#

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
C# eval equivalent?
Hi - I was wondering how I can execute a user provided string containing c# code from my application.
Example:
string userProvidedString = "Console.Write("Hello World!")";
ExecuteCode(userProvidedString); // Should output Hello World!

There's a CSharpCodeProvider Class in Framework that can be utilized.
It contains a CompileAssemblyFromSource method that Parses and compiles the code in a String.
You might also like checking this blog post on MSDN: http://blogs.msdn.com/b/dohollan/archive/2010/08/09/programmatically-invoke-the-c-compiler.aspx
NOTE: The above blog post link does not work anymore, so use this one instead: https://learn.microsoft.com/en-us/archive/blogs/dohollan/programmatically-invoke-the-c-compiler

Follow this method: http://www.west-wind.com/presentations/dynamiccode/dynamiccode.htm

Related

Undefine a keyword in C# [duplicate]

This question already has answers here:
How can I use a reserved keyword as an identifier in my JSON model class?
(3 answers)
How do I use a C# keyword as a property name?
(1 answer)
Closed 3 years ago.
I am currently working on an c# application that uses the spotify api. For the parsing of the Newtonsoft stuff I am using Newtonsoft.Json. But when receiving a track, the json includes a key explicit, and explicit is a keyword. So my question is, if there is a way to give the track class a member called explicit
Use the # prefix to escape reserved keywords.
var #explicit = ...

How to check a string for a question mark? [duplicate]

This question already has answers here:
How can I check if a string contains a character in C#?
(8 answers)
Closed 5 years ago.
I want to check if an inputted string contains a question mark.
Probably quite simple but I'm new to coding.
Use String.Contains() :
string myString = "Hello world?";
bool containsQuestionMark = myString.Contains("?"); // true
For future references, use MSDN, it's filled with good documentation.
Alternatively (to Rick's answer), if you are checking just for char occurance in a string you can use IndexOf(char):
bool containsQuestionMark = myString.IndexOf('?') != -1;
There are also some minor (negligible) performance differences between two approaches, depending on the framework version being used.

How to fetch records without case sensitive in MongoDb? [duplicate]

This question already has answers here:
MongoDB: Is it possible to make a case-insensitive query?
(27 answers)
Closed 8 years ago.
I have a collection like
{
"email" : "sh#Gmail.com"
}
My query should be like , it has to find matching email irrespective of case whether its capital or small but it should match exactly.
In sql we will do like where Lower(strEmail) = Lower(#emailParameterPassed) to satisfy this same.
UPDATE
I got it here.
Thanks
How to achieve this in mongoDb?
I am using c# native driver with mongoDb ?
Nonetheless the answer you are duplicating will give you a correct result, it is not a good idea to do queries like this. Why should you use regex (without indexes ) if you can use normal equal and take advantage of indexes?
There is no difference between sh#Gmail.com and sh#gmail.com so why not to store them in a canonical form in the first place and then to use normal search. To change all the documents, you can refer to my previous answer.

C# Canonical file names [duplicate]

This question already has answers here:
How can one get an absolute or normalized file path in .NET?
(4 answers)
Closed 9 years ago.
How to get canonical file name by non-canonical one.
E.g. I want to call function which converts "C:\Program files\..\Windows\aaa.txt" to "C:\Windows\aaa.txt"
I am looking for something like Java File.getCanonicalPath()
You can use the Path.GetFullPath method for this.
Example:
Console.WriteLine(Path.GetFullPath(#"C:\Program files\..\Windows\aaa.txt"));
Output:
C:\Windows\aaa.txt
System.IO.Path.GetFullPath("C:/Program files/../Windows/aaa.txt")
will return
"C:\\Windows\\aaa.txt"
Here is my suggestion:
string path = Path.GetFullPath(#"C:\Program files\..\Windows\aaa.txt");

Difference between bool/Boolean, string/String etc [duplicate]

This question already exists:
Closed 12 years ago.
Possible Duplicate:
String vs string in C#
What is the big difference between these datatypes? and were should I use it?
Short answer, there is no difference. They are just alias of each other, see
http://msdn.microsoft.com/en-us/library/ya5y69ds(VS.80).aspx for a complete list.
Have a look at: String vs string in C#

Categories

Resources