Dynamic Programming and alike in c# [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I need to learn a certain programming subject and I don't know where to start, would like your help.
This is what I need do do, I have a user form (UI) and the user enter "Rules" in the form of:
if operator(obj1) then assign(obj1,string)...
I take this rules, and translate them into actual code, and I want to put that code somewhere in a function/my code.
for example:
main {
UI...
/* when we reach here, means the user done writing rules */
/*Function that translate the user rules to actual code */
translate();
for {
/* This is where I want to put the code after translation */
}
}
How do I put the code inside the loop (or anywhere else for that matters) after the program started running?
I ofcourse don't look for an actual answer, more to give you an idea what I need so you can refer me to a certain subject to study about.

I presume, you are in process of creating a custom rule engine, which has the capability of validating your rules on fly. Within my ability, you need to start reading c# scripting, code generation, dynamic loading or reflection etc are some to start with.
To give a kick start, following are some of the step which I can think off;
Grab the rule definition (xml or csv)
Write a small helper which will read rule entries from the definition and convert it into
c# source code. This is similar to c# scripting.
On successful completion of (2), create a dll out of the source code
Now reflect/dynamically load the dll from (3) to where ever you wanted to validate the rule.

Related

Generate C code using C# [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I've got this task that requires me to generate some basic C code using a software written in C#.
The generated code should be based on some input files I provide to my software, we'll call it btOS for easy of communication.
So when starting btOS I give it as input file1, config.xml. When I hit run it should output a file.c that contains some basic structures and/or methods based on what the input files contain.
Is there any elegant way to do this ? Maybe some already generated templates or methods or stuff like that ? The only way I could think of handling this was creating specific strings in C# and outputting them to a C file.
L.E.: It seems that somehow my question was not clear enough. I assume the fault of including C++ in the title, I have remove it but I don't see how that is relevant because the question was very simple.
Anyway, to make it more clear. All i need to do is read some config files (their content is irrelevant, all they contain are some variables that will be used to generate some function templates, which will mostly impact the name of the function) - and write an output file with the extension .C (as in Main.c) that will contain those templates I generated.
So, again, the question: Are there any "elegant" and maybe somehow "professional" ways to do this other than using custom generated strings within the code that I will write to the file ? Right now the only way I see fit to do this without too much hassle is using some template text files with a naming convention defined by me(e.g. function_variableName{...}) where I just change the [variableName] text with whatever I need to to be there and "Abracadabra" I have a function that I will write to the file.
Now as Soonts suggested please try and be helpful, read multiple times if you don't clearly understand or maybe even don't bother - let somebody who is interested in this topic, tries to help or gain some new knowledge before flagging it.
Double Cheers.

Expression editor / parser / evaluator [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am trying to work on a C# project and I would like to allow the user to run code on the fly, by typing code into an editor.
So with that in mind, I thought about writing some sort of scripting editor, parser and evaluator, but wanted to check with other people first, in case I am just reinventing the wheel?
So, my idea is/was to...
Write a syntax highlighted editor that I can write code into,
including the use of custom keywords.
Add logic to the editor so that it will be able to format the
content based on the text in the editor.
Have a way of 'actioning' the text that has been entered.
For instance, if I enter the following...
if (Shape.IsACube())
{
// Do some cube stuff
}
else if (Shape.Area(height, length, width) > 40)
{
// Do some large area stuff
}
...then I would like to be able to run that code on the fly as though it was part of the application.
I hope all of that makes sense. Any thoughts?
Which grammar for your syntax you want to use? Your own or C# ?
If you just want to compile some C# code in runtime - you can use something like CodeDOM. Highlighting can be achieved through C# Syntax Highlighter.
If you want your own grammar with your terminals/non-terminals - you should use grammar makers like ANTLR4. This way you will understand how most compilers/interpretators works.
The main idea is that you write Lexer (which responsible for tokenization of your input) and Parser (which is actualy just list of grammar rules, 'productions' in other words). It will give you full AST from which you can evaluate/highlight your text. Many language grammars already written before you in here including C# syntax (even ANTLR4 itself is presented). So you can just pick some and modify it for your needs.

C# checking if a word is in an English dictionary? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am trying to go through a list of words and for each one determine if it is a valid English word (for Scrabble). I'm not sure how to approach this, do I have to go find a text file of all English words and then use file reading and parsing methods to manually build a data structure like a trie or hashmap - or can I find those premade somewhere? What is the simplest way to go about this?
You can use NetSpell library for checking this. It can be installed through Nuget Console easily with the following command
PM> Install-Package NetSpell
Then, loop through the words and check them using the library
NetSpell.SpellChecker.Dictionary.WordDictionary oDict = new NetSpell.SpellChecker.Dictionary.WordDictionary();
oDict.DictionaryFile = "en-US.dic";
oDict.Initialize();
string wordToCheck = "door";
NetSpell.SpellChecker.Spelling oSpell = new NetSpell.SpellChecker.Spelling();
oSpell.Dictionary = oDict;
if(!oSpell.TestWord(wordToCheck))
{
//Word does not exist in dictionary
...
}
Since you're looking specifically for valid Scrabble words, there are a few APIs that validate words for Scrabble. If you use anything that's not for that intended purpose then it's likely going to leave out some words that are valid.
Here's one, here's another, and here's a separate question that lists available APIs.
So that I can add some value beyond just pasting links, I'd recommend wrapping this in your own interface so that you can swap these out in case one or another is unavailable (since they're all free services.)
public interface IScrabbleWordValidator
{
bool IsValidScrabbleWord(string word);
}
Make sure your code only depends on that interface, and then write implementations of it that call whatever APIs you use.

Is it possible to make a program that reads its own source code? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
The community reviewed whether to reopen this question 3 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
What I mean is, could one possibly make a program that does the equivalent of
public class PrintOwnSourceCode
{
public static void Main ( )
{
System.Console.WriteLine([something]);
// prints "public class PrintOwnSourceCode { public static void Main ( ) { ... } }"
}
}
???
And would that be an example of reflection?
Somewhat.
Decompilers can do something similar to this:
I just decompiled a decompiler so I could use it to decompile itself
.NET Decompilers, like [.NET Reflector] (http://www.red-gate.com/products/dotnet-development/reflector/) and dotPeek are capable of reflecting upon a .NET assembly and generating files that resemble the source code. It will not look exactly like the source code because compiling and decompiling is kind of like translating English to French and then back to English--the results are not always guaranteed to be 1:1 as Google Translate can demonstrate. Information, like whitespace, that are for easy reading but not required by the compiler will be lost in the decompilation process. So, your application could decompile itself or invoke an external decompiler to print itself.
Aside
In compiled languages, the compiled code does not have direct access to the source code. (Companies don't typically ship the source code with the compiled code to customers. They only ship the compiled executable.) When it comes to parsed languages, like JavaScript, it's a whole different story. Because the source must be available to the runtime so that it can be parsed and run, the code can always find it's own source file, open it, and print it out.
This was answered here.
The short answer is that you cannot print it via reflection.
If you want to print out the file, then you will need to load in the source file itself (and have the file available).

C# Windows Form - access code [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I need to know where I can access the basic code, for example a "NumericUpDown" Element.
What I want to do:
Add Code to my NumericUpDown Element, for example:
Set the "Maximum"-Value to a variable embedded inside my Formclass code.
The Reason why I cant to it right now:
I just can access the NumericUpDown Element just in the Design View right now. There I can Input a value, but can't set it to the amount stored in a variable.
I just know, that If I double click the field, I get a "ValueChanged" Method into my code. But what I need to know: How can I access the "basic" Code of the NumericUpDown Element, where I could set the "Maximum"-Property?
I'm relatively new to programming. And I'm quite overwhelmed with a lot of things... So I COULD google for the problem but it would just take me way to much time.
Since I guess it's an easy answer for you guys, I post it here.
Thanks a lot!
You can access the Control's objects attributes by it's name.
For example:
NumericUpDown1 <- It's the name of your Control
In your C# code you can access all of it's attribtes and methods by puttin a period after it's name:
NumericUpDown1.Maximun = 100;
NumericUpDown1.Width = 250;
NumericUpDown1.Height = 10;
etc. You can serach it by "Control Properties" in google. Luck and effort! :P Remember to try and search a bit before asking. Don't wait for others to make your job. Luck! ^.^
You can access source code of components, but it won't help you. every component has interface and you can use it's interface to interact with it. Also you can override some methods of component to change their behavior.

Categories