Our department has inherited two new code bases. One is in C#, the other is in Objective C. The first has a bunch of functionality that we need in the second as well.
I realize that there isn't going to be a 1-to-1 relationship that we can run a simple translator to move from C# to Objective C, but is there some tool to do a rough conversion of the syntax.
We're mostly looking for a tool that would do some of the mindless part. I'm not looking forward to manually translating 1000 function headers, for example, when the format for both is so well defined.
EDIT
Even something we could run on an individual methods one at a time would speed up the process significantly.
Are you getting rid of the C# code base? If not then it seems like you can look at this from a different angle - rather than undertaking the huge effort of converting thousands of classes that rely on probably scores of API's, how about exposing your existing functionality as services that can be called from your Objective-C code? You can do this using web services.
Alternatively you can take a look at Mono/Cocoa#, though I doubt this is going to be a viable solution for your problem.
Related
We are developing a product that will be deployed for a number of clients. In an area of this product, we need to execute some formulas. The problem is that this formulas can be different for each client. So we need to have a good architecture that can 'execute code dynamically'
The application is a web application hosted on sharepoint 2010 (thereby using .net 3.5)
A Simplified example:
I have class MyClassA with two numeric properties PropA and PropB
For one client the formula is PropA + PropB. For the other it is PropA-PropB.
This is just a simplified example as the formula is more complex than this.
I need to have a way that client A we can set PropA+PropB perhaps in an XMl file or database.
I would then load the code dynamically?
Does this make sense? Has anyone implement similar scenario in a production environment please?
I have found that the following article describes a similar situation but I do not know whether it is 100% reliable for a production environment:
http://west-wind.com/presentations/dynamicCode/DynamicCode.htm
I have also found that IronPython can also solve a similar problem but I cannot understand how I would use my ClassA with IronPython.
Any assistance would be greately appreciated.
Update ...
Thanks everyone for the detailed feedback. It was a very constructive exercise. I have reviewed the different approaches and it seems very likely that we will go ahead with the nCalc approach. nCalc seems to be a wonderful tool and I am already loving the technology :)
Thank you all!!
Look into nCalc.
You could store your calculations in a database field then simply execute them on demand. Pretty simple and powerful. We are using this in a multi-tenant environment to help with similar types of customization.
I'm just proposing this because I don't know the problem very well but the idea could be a Dll for each formula (so you can handle the code as you wish, with normal C# functionalities instead of an uncomfortable xml file).
With MEF you can inject dll into your code (you just have to upload those when you develop a new one, no need to recompile the exe file) and have a different formula for each client.
This is my idea because it looks like a perfect example for Strategy pattern
Do you have a fixed set of formulas, or does the client have the capacity to dynamically type those in, i.e. as for a calculator?
In the first case, I'd recommend the following: set of C# delegates, which get called/call each other in a particular order, and (a) Dictionary(ies) of closures which fit the delegates. The closures would then be assigned to the delegates based on your predefined conditions.
In the alternative case, I wouldn't compile .NET code based on what the client types in, since that (unless preempted) represents a server-side security risk. I would implement/adapt a simple parser for expressions that you're expecting.
P.S. nCalc sugguested by Chris Lively is definitely a viable option for this kind of task, and is better than directly using delegates if you have tons and tons of formulas that you don't want to keep in memory.
ClassA with Ironpython?
Keep it simple
Run through the classA instance for each member (maybe a custom attribute to mark up the ones you want to use in the calc) and end up with name=value pair which by some unfortunate coincidence looks like an assignment
e.g
PropA = 100
PropB = 200
Prepend that to your python script
PropAPropB = PropA + PropB
Execute the script which is the assignments and the calculation
Then it's basically
ClassB.PropAPropB = ipCalc.Eval("PropAPropB");
You can start getting real clever with it, but a methods to get the inputs from an instance and one the evaluatios the result of the calc and sets teh properties.
Bob's your mother's sister's brother...
I'm trying to write a visualiser for some code which generates graphics for barcodes and labels. The way I want to do this is by recording the methods+parameters being run to a file, so I can play them back and see the visual output generated at each stage (so a kind of visual debugger to help me fix issues with measurements in the drawing)
I have access to the methods, and I can put anything I like in them - but I'm stuck on the best way to record the method signature being called and the parameters, especially since a lot of them are overloads etc.
Is there anything simple that will help me serialize/record actual method call information? (with a view to replay it back, so I need to programmatically load the information and call it) Perhaps something reflection-related?
Note: I'm an intern on the project I'm working on, and I'm probably not allow to introduce new assemblies etc. into the build, so I think aspect-based things requiring libraries are out. (At the same time, I'm not just asking a Q. I should be figuring out myself - this is more an additional thing I'm doing during my lunch break to help my main task)
It might be a good idea to start from an existing profiler as a base - e.g. from http://code.google.com/p/slimtune/
Note that profilers themselves are quite complicated - for .Net they require some C++/COM knowledge - but if you start from a base like slimtune, then hopefully you'll be able to avoid this core code and will instead be able to focus on your own visualisation requirements.
Recording the method name itself is easy, parameters will be more difficult. I think the only way to generically retrieve the parameters is to use reflection--the alternative is to have an ungodly amount of logging code where you explicitly log every parameter.
Also consider that you'll need all parameters to be serializable, and depending on how you want the file to be used (by a program vs. human readable) you might have to implement quite a bit of boilerplate serialization code.
You should really consider existing profiling tools and testing tools rather than thinking of inventing something new. It sounds like performance tests or integration tests may be more valuable than a "playback" utility.
I would like to build an application framework that is mainly interpreted.
Say that the source code would be stored in the database that could be edited by the users and always the latest version would be executed.
Can anyone give me some ideas how does one implement sth like this !
cheers,
gabor
In .Net, you can use reflection and CodeDOM to compile code on the fly. But neither approach is really very simple or practical. Mono has some ability to interpret c# on the fly as well, but I haven't looked closely at it yet.
Another alternative is to go with an interpreted .Net language like Boo or IronPython as the language for your database code.
Either way, make sure you think long and hard about the security of your platform. Allowing users to execute arbitrary code is always an exercise fraught with peril. It's often too tempting to look for a simple eval() method, and even if one exists, that is not good enough for this kind of scenario.
Try Mono ( http://www.monoproject.org ). It supports many scripting languages including JavaScript.
If you don't want to use any scripting you can use CodeDOM or Reflection (see Reflection.Emit).
Here are really useful links on the topic :
Dynamically executing code in .Net (Here you can find a tool which can be very helpul)
Late Binding and On-the-Fly Code
Generation Using Reflection in C#
Dynamic Source Code Generation and
Compilation
Usually the Program uses a scripting language for the scriptable parts, i.e. Lua or Javascript.
To answer your technical question: You don't want to write your own language and interpreter. That's too much work for you to do. So pick some other language, say Python or Lua, and look for the documentation that lets your C program hand it blocks of code to execute. Of course, the script needs to be able to do something, so you'll need to find how to expose your program's objects to the script. Also, what will happen if a client is running the program when you update its source code in the database? Should the client restart? Are you going to store the entire program as a single row in this database, or did you want to store individual functions? That affects how you structure your updates.
To address other issues with your question: Why do you want to do this? Making "interpreted language" part of your design spec for a system is not often a good sign. Is the real requirement something like this: "I update the program often and I want users to always have the latest copy?" If so, there are other, better ways to go about this (just give us your actual scenario and requirements).
I am currently developing an application where you can create "programs" with it without writing source code, just click&play if you like.
Now the question is how do I generate an executable program from my data model. There are many possibilities but I am not sure which one is the best for me. I need to generate assemblies with classes and namespace and everything which can be part of the application.
CodeDOM class: I heard of lots of limitations and bugs of this class. I need to create attributes on method parameters and return values. Is this supported?
Create C# source code programmatically and then call CompileAssemblyFromFile on it: This would work since I can generate any code I want and C# supports most CLR features. But wouldn't this be slow?
Use the reflection ILGenerator class: I think with this I can generate every possible .NET code. But I think this is much more complicated and error prone than the other approaches?
Are there other possible solutions?
EDIT:
The tool is general for developing applications, it is not restricted to a specific domain. I don't know if it can be considered a visual programming language. The user can create classes, methods, method calls, all kinds of expressions. It won't be very limitating because you should be able to do most things which are allowed in real programming languages.
At the moment lots of things must still be written by the user as text, but the goal at the end is, that nearly everything can be clicked together.
You my find it is rewarding to look at the Dynamic Language Runtime which is more or less designed for creating high-level languages based on .NET.
It's perhaps also worth looking at some of the previous Stack Overflow threads on Domain Specific Languages which contain some useful links to tools for working with DSLs, which sounds a little like what you are planning although I'm still not absolutely clear from the question what exactly your aim is.
Most things "click and play" should be simple enough just to stick some pre-defined building-block objects together (probably using interfaces on the boundaries). Meaning: you might not need to do dynamic code generation - just "fake it". For example, using property-bag objects (like DataTable etc, although that isn't my first choice) for values, etc.
Another option for dynamic evaluation is the Expression class; especially in .NET 4.0, this is hugely versatile, and allows compilation to a delegate.
Do the C# source generation and don't care about speed until it matters. The C# compiler is quite quick.
When I wrote a dynamic code generator, I relied heavily on System.Reflection.Emit.
Basically, you programatically create dynamic assemblies and add new types to them. These types are constructed using the Emit constructs (properties, events, fields, etc..). When it comes to implementing methods, you'll have to use an ILGenerator to pump out MSIL op-codes into your method. That sounds super scary, but you can use a couple of tools to help:
A pre-built sample implementation
ILDasm to inspect the op-codes of the sample implementation.
It depends on your requirements, CodeDOM would certainly be the best fit for a "program" stored it in a "data model".
However its unlikely that using option 2 will be in any way measurably slower in comparision with any other approach.
I would echo others in that 1) the compiler is quick, and 2) "Click and Play" things should be simple enough so that no single widget added to a pile of widgets can make it an illegal pile.
Good luck. I'm skeptical that you can achieve point (2) for anything but really toy-level programs.
I need to import some ANSI C code into a project I'm working on. For reasons I prefer not to go into, I want to refactor the code to C# rather than trying to wrap the original code. It will take me perhaps a couple of days to do the work by hand, but before I start, is there an automated tool that can get me most of the way there? I'm a cheapskate and the work I'm doing is pro bono, so free tools only please.
If manual refactoring is "only" going to take a few days, that would get my vote. Depending on what the C code is doing and how it is written (pointers, custom libraries, etc.) an automated converter may just make a mess. And untangling that mess could be a larger task than just converting by hand.
Setting up, cleaning up, refactoring, and just plain making converted code (if there is even a converter available) would probably be something in the magnitude of weeks or months, so you'll be better served just to go ahead with the manual rewrite.
There is an experimental CLI Back-End and Front-End for GCC. It is already capable of compiling a subset of C programs into CIL, the byte-code that the CLR runs.
(The webpage makes it seem like the code was only developed over a few months and then ignored since then, but it's out of date; ST Microelectronics is continuing maintenance and development.)
You don't specify why you want a C to C# translator, but if you just want to get C and C# to play together without P/Invoke or COM, it might be good enough.
It may make sense to start by getting the existing code to compile as managed C++ aka C++/CLI. Assuming that went smoothly enough, then you have a working, testable foundation on which to build. Move key features to their own classes, and as needed, rewrite as C# along the way.