Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Basically I have two questions:
1) How to emit or generate a IronPython code (tools, libs) in a C# application. The result of this process should be string consisting of real IronPython code not IL code.
2) How beneficial is the approach above over generating a IronPython code on your own (simply by using StringBuilder)?
I am looking for some code generator library similar to this IMAGINARY pseudo code generator:
IronPythonCodeGenerator generator = new IronPythonCodeGenerator();
Parameter param = new Parameter("str");
ParameterValue value=new ParameterValue(param,"This piece is printed by the generated code!!!");
Function function = IronPythonCodeGenerator.CreateFunction("PrintExtended",param);
function.AppendStatement("print",param);
function.AppendStatement(Statements.Return);
FunctionCall functionCall = new FunctionCall(function,value);
generator.MainBody.Append(function);
generator.MainBody.Append(functionCall);
Console.WriteLine(generator.MainBody.ToString());
, which outputs the IronPython code:
def PrintExtended( str ):
print str;
return;
PrintExtended("This piece is printed by the generated code!!!");
Reflection.Emit is for generating IL code, not for generating high-level language code. So if your target language is IronPython, building it up in a StringBuilder is probably your best bet.
It of course depends on your needs. If all you want to do is just generate code without wanting to change the order of methods, or modify methods after they've been defined etc., just constructing code in a StringBuilder and then compiling it would be the easiest way.
Related
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.
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'm trying to disassemble my C# code and then debug it on assembly language level.
Let's say we have a simple C# method:
var a = 1235;
var b = ++a;
var c = ++b;
Console.WriteLine("test");
Console.ReadKey();
I've found two different ways how to get an assembly code.
The first one is to start C# code debugging in VS and then open Disassembly window. Here we have the following code.
Everything is OK and assembly code is pretty much simple and short but the problem is that the logic of this assembly code differs from the logic of the IL code generated by ildasm.
So here is the second way. We can compile C# code, use ildasm to get the IL code from PE file and then use ilasm to generate PE file back. Now we have the following assembly code.
As you can see this assembly code is more like the IL code but it contains much more instructions and it is more complicated.
AFAIK C# compiles to CIL code and then to an assembly code in both cases. But it seems to be that in the first way it just compiles to an assembly code.
So the question is why the assembly code of the first method differs from the IL code? And why the assembly code of the first method differs from the assembly code of the second method?
The JIT is able to re-order and merge machine instructions as an optomisation, but will try to avoid moving the effects across sequence points provided by the pdb. The compiler generally generates one sequence point per line of code, since you generally step through it one line at a time.
While C# will often generate multiple IL instructions per line of code, ilasm is given each instruction explicitly and so generates more sequence points, leaving less room for JIT optimizations.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
//
string productName;
byte& local1 = (byte&) productName;
//
What is this byte&?
I really don't understand. I got code from my friend but I don't understand what this line wants to tell? Because it gives error or redline in my VS2012. Can anyone explain?
It's making local1 a reference byte type, but that's probably coming from some IL decompilation and not valid C# syntax. That result code will more likely happen if you decompile a IL function with a ref string parameter.
You can't compile that code using a C# compiler. There are many things that are legal in IL but there's no C# equivalent syntax, and that's usually the way it's decompiled back (which doesn't make it compilable) in a best-effort to make it look like C#
It is invalid. The closest syntax is byte? - which adds null support to byte.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a Delphi created DLL that has functions I need to call from VS 2013 C3 app.
Not exactly sure how or where to start to accomplish this.
Do I have to include the dll as a reference or import it somehow or both?
And how do I call the program?
The dll is MSA.dll and the method I need to call looks like this:
GetXML(txtPath.Text, txtCabFile.Text, False);
Any 101 basic suggestion appreciated.
Check out something called "P/Invoke." It allows you to call into "native" (i.e. Delphi, C, etc.) DLLs using simple "extern" function definitions.
Here's a website I use as a resource for P/Invoke calls to the Windows API:
http://www.pinvoke.net/
EDIT: Make sure your target on the .NET side is the same as the one you compiled your Delphi library in. When in doubt, its probably x86 if you're on a PC. Thanks to the commenter below who brought this point to my attention. Don't use AnyCPU.
EDIT 2: The extern declaration you would use:
[DllImport("MSA.dll", CharSet = Ansi)]
public extern string GetXML(string firstParam, string secondParam, bool thirdParam);
You can name the parameters whatever you want. I didn't know what to call them so I just gave them names.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm working on a project that combines a very large open source project (OSP) written in C and attempting to build a front end in C#. I'm currently compiling the OSP with Visual Studio 2012 Express and generating all the .exe's, .dll's,etc from this project.
How should I perform the integration? (Remember I have full access to the .h/.c files) I attempted using the IJW (It Just Works) method, but it didn't appear to allow me to import the references for my freshly compiled .dll's. Apart from that, I'm not sure how to execute the various functions within C# (apart from sending command line commands to the .exe's, which I would prefer not to do....)
Do I need to compile the OSP with special options/parameters for IJW, or would it require code rewrite?
For DLL written in C, you need to write the equivalent declarations in C# instead of adding reference to that DLL.
This is called PInvoke.
class ABC
{
[DllImport("abc.dll")]
public static extern int FuncX(int x, int y);
}