Loading an whole Python project when hosting IronPython in C# - c#

I've seen that there is documentation about how to execute or compile a Python script in C# by reading about Hosting approach for IronPython inside a .Net project: http://www.voidspace.org.uk/ironpython/dlr_hosting.shtml
But nothing is explaining clearly how to load an whole Python/IronPython project that contains several folders and files with a lot of import.
Is this something possible, or this approach is not appropriate for large project, and more for short scripts?

Related

how to explicitly link a cpp/cli file to a c# library .dll?

I have a c++/CLI library that is in turn calling a c# library. That is fine, it is linking implicitly and all is good with the world. But for various reasons the libraries are not getting quite the prefect treatment by our automated build process, and the libraries are not finding each other unless we move the libraries to locations that we would rather not have them in, and would rather not fold into our build process.
It is suggested to me that we/I could write a post-build event that uses XCOPY. but lets say we don't want to do that.
Another suggestion is to explicitly load the dll. Windows says that to link explicitly "Applications must make a function call to explicitly load the DLL at run time." The problem is that Microsoft's example is not enough for my small mind to understand how to proceed with this idea. Worse, the only example I could find is out of date. Perhaps I am not using the right search terms but I am having difficulty finding more about it with google.
How do we explicitly Link a c++/Cli Library to a C# .dll?
----edit
OK, How do we explicitly Link a C++/CLI code, which exports a library using __declspec(), to a C# .dll.
There is no such thing as a "C++/CLI library", only assemblies are supported. There is no explicit or implicit linking, binding always happens at runtime. Assemblies are found at runtime by the CLR, the rules it uses to locate them are described in detail in the MSDN library.
Copying all dependencies into the same directory as the EXE is the sane way to go about it while you are developing the code. Well supported by build system, the C# and C++ rules are however different. C++ projects build to the solution's Debug directory, C# projects build to the EXE project's bin\Debug directory. So yes, altering a C++ project's Output Directory setting or copying files with a post build event is usually required to get everything together.

can ironruby nuget package be installed then used in a vanilla VS2012

I am interested in using a ruby script (https://github.com/zmoazeni/csscss) withing an ASPNET MVC site. I have no previous experience with ruby so my question could be naive.
Is it possible to import the ironruby nuget package and execute the ruby scripts?
My goal would be to put a .net wrapper around the ruby script(s) and put the output out as HTML.
So far I have tried Googling getting started with ironruby but have not found much that I understand and could get working, (yes that sound vague, but I don't know ruby or ironruby and couldn't find any good simple guides on getting started).
I've realized a simple proof of concept at https://github.com/edymtt/csscss-from-ironruby that shows in a console application how to use IronRuby to run the source code of csscss to analyze a CSS loaded from a file. To achieve this result I've started from this SO question -- you can find the additional resources I've used in the comments of the program. I haven't tried this code in a ASP.net MVC site -- anyway this sample should be a good starting point.
This solution is a bit cumbersome to maintain, since you have to manually put the sources for the csscss and its dependent libraries in the solution. An alternative solution is to install Ruby on the machine, install csscss using gem(so it download the dependencies automatically) and to invoke the program from .NET -- I'll also show this approach in the sample. Note that this solution requires that you could install Ruby on the web server.
UPDATE 2013-09-02 18:15 UTC Following the suggestion from Zach Moazeni I've been able to semplify the approach that used IronRuby to run csscss and I've updated accordingly the proof of concept. In a nutshell:
outside the .NET program I've used bundler to download csscss and json (and dependent gems) to a local folder of the project;
in the .NET program I've written a function to discover all the paths of the libraries in the gem folder created by bundler (by finding the gems folder and then including for each subfolder the lib folder, this algoritm was inspired by this SO thread);
I've passed this list of paths to the IronRuby interpreter before launching csscss.
This approach should conjugate the ability to use only .NET to run the program with the ease of the update given by gem and bundler.
I'm the author of csscss and I built the JSON output for this reason.
csscss -j file.css
It's not a perfect solution, but instead of outputting human readable text, it will output JSON that you can parse from whatever language/runtime.

How to compile c#/c++ files from Java Application

I am just looking into compilers and I was wondering is it possible to compile both c# and c++ files from a Java Application (e.g. to compile java from a java application you can use the JavaCompiler API). I have looked online for this but all i can find is ways to compile java files from c# and c++ and not the other way around.
If so, what API's can you use for this?
If you know the system commands for compiling and executing .cpp files(don't know much about c#) you might want to check out this. It details how to execute system commands from a Java program. Pass the system commands for compiling the required file in Runtime.getRuntime().exec().
Consider learning how to call ant from Java code and using something like this ant enhancement.
Disclaimer: I don't know anything about this product, but found it by searching for "can ant build c++?"
For C# in Windows: compiler (csc.exe) is part of .Net install on Windows and can be found at well known location (like %windir%\Microsoft.NET\Framework\v3.5 for .Net 3.5). The same place also contains MSBuild.exe that can build project files (*.csproj).
Your code may need to provide locations for referenced libraries if using Csc.exe to compile individual files.

Mixing C# & VB In The Same Project

Can you mix vb and c# files in the same project for a class library? Is there some setting that makes it possible?
I tried and none of the intellisense works quite right, although the background compiler seems to handle it well enough (aside from the fact that I, then, had 2 classes in the same namespace with the same name and it didn't complain).
We're trying to convert from VB to C# but haven't finished converting all the code. I have some new code I need to write, but didn't really want to make a new project just for it.
No, you can't. An assembly/project (each project compiles to 1 assembly usually) has to be one language. However, you can use multiple assemblies, and each can be coded in a different language because they are all compiled to CIL.
It compiled fine and didn't complain because a VB.NET project will only actually compile the .vb files and a C# project will only actually compile the .cs files. It was ignoring the other ones, therefore you did not receive errors.
Edit: If you add a .vb file to a C# project, select the file in the Solution Explorer panel and then look at the Properties panel, you'll notice that the Build Action is 'Content', not 'Compile'. It is treated as a simple text file and doesn't even get embedded in the compiled assembly as a binary resource.
Edit: With asp.net websites you may add c# web user control to vb.net website
Well, actually I inherited a project some years ago from a colleague who had decided to mix VB and C# webforms within the same project. That worked but is far from fun to maintain.
I decided that new code should be C# classes and to get them to work I had to add a subnode to the compilation part of web.config
<codeSubDirectories>
<add directoryName="VB"/>
<add directoryName="CS"/>
</codeSubDirectories>
The all VB code goes into a subfolder in the App_Code called VB and the C# code into the CS subfolder. This will produce two .dll files. It works, but code is compiled in the same order as listed in "codeSubDirectories" and therefore i.e Interfaces should be in the VB folder if used in both C# and VB.
I have both a reference to a VB and a C# compiler in
<system.codedom>
<compilers>
The project is currently updated to framework 3.5 and it still works (but still no fun to maintain..)
You can not mix vb and c# within the same project - if you notice in visual studio the project files are either .vbproj or .csproj. You can within a solution - have 1 proj in vb and 1 in c#.
Looks like according to this you can potentially use them both in a web project in the App_Code directory:
http://pietschsoft.com/post/2006/03/30/ASPNET-20-Use-VBNET-and-C-within-the-App_Code-folder.aspx
It might be possible with some custom MSBuild development. The supplied .targets force the projects to be single language - but there's no runtime or tooling restriction preventing this.
Both the VB and CS compilers can output to modules - the CLR's version of .obj files. Using the assembly linker, you could take the modules from the VB and CS code and produce a single assembly.
Not that this would be a trival effort, but it probably would work.
Walkthrough: Using Multiple Programming Languages in a Web Site Project http://msdn.microsoft.com/en-us/library/ms366714.aspx
By default, the App_Code folder does not allow multiple programming languages. However, in a Web site project you can modify your folder structure and configuration settings to support multiple programming languages such as Visual Basic and C#. This allows ASP.NET to create multiple assemblies, one for each language. For more information, see Shared Code Folders in ASP.NET Web Projects. Developers commonly include multiple programming languages in Web applications to support multiple development teams that operate independently and prefer different programming languages.
Yes its possible.adding c# and vb.net projects into a single solution.
step1: File->Add->Existing Project
Step2: Project->Add reference->dll or exe of project which u added before.
step3: In vb.net form where u want to use c# forms->import namespace of project.
Although Visual Studio does not support this (you can do some tricks and get MSBuild to compile both, but not from within Visual Studio), SharpDevelop does. You can have both in the same solution (as long as you are running Visual Studio Professional and above), so the easiest solution if you want to keep using Visual Studio is to seperate your VB code into a different project and access it that way.
Why don't you just compile your VB code into a library(.dll).Reference it later from your code and that's it. Managed dlls contain MSIL to which both c# and vb are compiled.
Right-click the Project. Choose Add Asp.Net Folder.
Under The Folder, create two folders one named VBCodeFiles and the Other CSCodeFiles
In Web.Config add a new element under compilation
<compilation debug="true" targetFramework="4.5.1">
<codeSubDirectories>
<add directoryName="VBCodeFiles"/>
<add directoryName="CSCodeFiles"/>
</codeSubDirectories>
</compilation>
Now, Create an cshtml page.
Add a reference to the VBCodeFiles.Namespace.MyClassName using
#using DMH.VBCodeFiles.Utils.RCMHD
#model MyClassname
Where MyClassName is an class object found in the namespace above.
now write out the object in razor using a cshtml file.
<p>#Model.FirstName</p>
Please note, the directoryName="CSCodeFiles" is redundant if this is a C# Project and the directoryName="VBCodeFiles" is redundant if this is a VB.Net project.
I don't see how you can compile a project with the C# compiler (or the VB compiler) and not have it balk at the wrong language for the compiler.
Keep your C# code in a separate project from your VB project. You can include these projects into the same solution.
You need one project per language. I'm quite confident I saw a tool that merged assemblies, if you find that tool you should be good to go. If you need to use both languages in the same class, you should be able to write half of it in say VB.net and then write the rest in C# by inheriting the VB.net class.
At the risk of echoing every other answer, no, you cannot mix them in the same project.
That aside, if you just finished converting VB to C#, why would you write new code in VB?
For .net 2.0 this works. It DOES compile both in the same project if you create sub directories of in app code with the related language code. As of yet, I am looking for whether this should work in 3.5 or not though.
As others have said, you can't put both in one project. However, if you just have a small piece of C# or VB code that you want to include in a project in the other language, there are automatic conversion tools. They're not perfect, but they do most things pretty well. Also, SharpDevelop has a conversion utility built in.
No, not in the same project.but you can use them in the same solution.
though you need to take care that your code is CLS compliant. That means you must not have used such functionality/feature that is not understand by other language. For example VB does not understand unsigned ints.
In our scenario, its a single VB.NET project (Windows Desktop application) in a single solution. However we wanted to take advantage of C# features like signed/unsigned integers and XML literals and string features in VB.NET. So depending on the features, at runtime we build the code file, compile using respective Rosalyn compiler (VB/CS) into DLL and dynamically load into current assembly. Of course we had to work on delinking, unloading, reloading, naming etc of the dynamic DLLs and the memory management were we largely used dynamic GUID for naming to avoid conflict.
It works fine where app user may connect to any DB from our desktop app, write SQL query, convert the connection to LINQ connection and write LINQ queries too which all requires dynamic building of source code, compiling into DLL and attaching to current assembly.
Yes, You can add both of the file in web site only.If the project is a web application it will not allow different type of file.

Packaging script source files in IronPython and IronRuby

Does anyone know how to add python and ruby libs as a resource in a dll for deployment? I want to host a script engine in my app, but dont want to have to deploy the entire standard libraries of the respective languages in source files. Is there a simple way to do this so that a require or import statement will find the embedded resources?
You could add custom import hook that looks for embedded resources when an import is executed. This is slightly complex and probably not worth the trouble.
A better technique would be to fetch all of the embedded modules at startup time, execute them with the ScriptEngine and put the modules you have created into the sys.modules dictionary associated with the engine. This automatically makes them available for import by Python code executed by the engine.
You can create StreamContentProviders for example
In the ironrubymvc project under IronRubyMVC/Core/ you will find what you need.
AssemblyStreamContentProvider
Usage of the ContentProvider
IronPython 2.0 has a sample compiler called PYC on Codeplex.com/ironpython which can create DLL's (and applications if you need them too).
IronPython 2.6 has a newer version of PYC under Tools\script.
Cheers,
Davy

Categories