when I compile some .py codefiles with no class definitions into dlls , the compiled dll is created with a "DRLCachedCode" class inside. Why is that?
When you compile IronPython code it doesn't get compiled to normal .NET code where you'd have a class at the IL level for each class you have at the source level. Instead it gets compiled into the same form that we compile to internally using the DLR.
For user code this is just a bunch of executable methods. There's one method for each module, function definition, and class definition. When the module code runs it executes against a dictionary. Depending on what you do in the module the .NET method may publish into the dictionary a:
PythonType for new-style classes
An OldClass for old-style classes
A PythonFunction object for function
definitions
Any values that you assign to (e.g.
Foo=42)
Any side effects of doing exec w/o providing a dictionary (e.g. exec "x=42")
etc...
The final piece of the puzzle is where is this dictionary stored and how do you get at it? The dictionary is stored in a PythonModule object and we create it when the user imports the pre-compiled module and then we execute the module against it. Therefore this code is only available via Python's import statement (or the extension method on ScriptEngine "ImportModule" which is exposed via IronPython.Hosting.Python class).
So all of the layout of the code is considered an internal implementation detail which we reserve the right to change at any point in time.
Finally the name DLRCachedCode comes because the DLR (outer layer) saves this code for us. Multiple languages can actually be saved into a single DLL if someone really wanted to.
This link answers the question: http://www.ironpython.info/index.php/Using_Compiled_Python_Classes_from_.NET/CSharp_IP_2.6 how to access an IronPython class from C#.
Manual compilation: \IronPython 2.7\Tools\Scripts>ipy pyc.py /out:MyClass /target:dll MyClass.py did not work. Only when I used SharpDevelop with IronPython it worked as in the post.
Related
I've created a small application. The GUI is ugly, I've done it quickly. I've created a ui.qml file with Qt Design Studio.
My question is : is it simple to use my ui.qml file instead of my ui file ? I mean, do I have to change all my code when I call ui->ComponentName ?
I tried to understand how ui.qml files works, but it seems it's a completely different way to use component in C++ files than ui files..
Depending on the build system you are using, files ending with .ui are used by autouic to generate C++ code that you can call to create your user interface, usually from the constructor of your class that will be the parent for those components.
This is done by calling setupUi on the member variable 'ui', which is the same class name as the parent class, just under the Ui namespace.
QML code is interpreted, however, and can be read by QQmlApplicationEngine. You can expose properties with setContextProperty, taking a QString and a QObject*, as explained further here: https://doc.qt.io/qt-6/qtqml-cppintegration-exposecppattributes.html#exposing-methods-including-qt-slots
How do I set system properties for raw Java classes used from C# code through IKVM?
I am dealing with some Java code that has been ported to C# using IKVM. Some of the classes have been wrapped in C# classes, but not all of the Java API yet. So I have two versions of some classes, and because only a small part of the API has been wrapped, I have to use the raw Java classes directly in my C# code.
When I use the C# wrapped version, I can parse UTF-8 encoded XML files correctly. When I try to use the underlying Java class directly, I get parsing errors ("content not allowed in prolog") which indicate the wrong charset is being used to parse.
In Java we solve encoding issues by setting -Dfile.encoding=UTF-8, and I am trying to do the same in C# as follows:
static FeedSample()
{
java.lang.System.setProperty("file.encoding", "UTF-8");
}
This setting is picked up when I use the C# wrapper class. When I use the underlying Java class directly, the system property is not picked up. I think I am missing something obvious here. I also tried putting -Dfile.encoding as a command line argument but that did not help.
I am new to the usage of reflection in Java/scala. It is not quite clear to me why we need to instantiate a type at runtime. An example would be the best. Thanks a lot.
I will give you a general example of where runtime type instantiation or in general inspection of a types is useful. Think of the Plugin Pattern. Assume you want to create an application that allows users to create plugins. You don't have the plugins the users are going to make in the future, at hand. How are you able to use their plugins after you have released your application? You need to be able to inspect their plugins for a method your application requires and then call said method.
In order to enable this, language designers create a platform in which you are able to query a module (jar in java, assemblies in .net) for the types it defines and the methods, fields, etc it contains. You can then call any method, instantiate any type you want and basically interact with the module as if you had the module at compile time and you were referencing it(well not exactly but you get the point).
Here's and example of a method call that happens at runtime. You can assume that we have already created foo from a string we get from a configuration file at runtime. foo was specified as the name of the jar file containing the plugin types. I don't want to provide the instantiation code as it would make this too bloated, but here is the method:
Method method = foo.getClass().getMethod("doSomething", null);
method.invoke(foo, null);
As you see, we basically got the type of the class foo, we queried it for a method using the method's name and then called it. By doing so we extended the functionality of our program with the plugin at runtime.
According to the answers to this question here, the reason why I'm not seeing errors as I work in Flash Builder is that FB is "optimizing" them out because they aren't referenced at any point in the code execution. Is there an option to force Flash Builder to compile all files regardless of whether they're used in the software? This would make my development process a lot more intuitive.
The only way to do this is to actually reference the class somewhere in code that you know actually is being compiled, such as the Document Class in a .fla, or your Main.as file in a pure AS3 project. It can be as simple as declaring a variable of the given type, even if no value is ever assigned to it.
private var complieMe:OtherwiseUnreferencedClass;
// ^ This will cause your class to be compiled.
You need to reference each class somewhere in your project. The easiest/shortest way I've found to do this is to add an import followed by the class name in some common place, such as a script block in Main.mxml, although it really doesn't matter where:
import some.package.MyClass; MyClass;
Hope that helps.
I'm doing some refactoring and am trying to reuse my genertated entity models. My application has a few assemblies, one being my outward facing public types (API) and one containing implementations of providers (such as the log).
I'd like to split the generation of the entities and models so that the entities will be in the API assembly and the container will be in the implementation assembly. Is this possible?
Is possible. This is how I did it.
Assembly A
Database.EDMX
Models.TT
Models.cs
Assembly B
Database.EDMX (Added as a Link to the real file in Assembly A)
EntityContainer.TT
EntityContainer.cs
That's how everything is laid out. These are the rough steps:
Right click on the EDMX in A (public API assembly) and Add Code Generation File
Adds a TT to the project. Called it Models, as it will contain the models only.
Edited the TT and removed code generation for entity containers
In assembly B (internal implementations) added Database.EDMA as a link
Opened in assembly B, right click and Add Code Generation File
Adds a TT to project B. Called it EntityContainer as it will contain that only.
Edited TT to do the following
Removed entity creation steps
Changed the path to Database.EDMX to a relative path pointing at the original copy in A
Added a using for my models
Hopefully this will all compile and work correctly (I'm still far from getting everything compiled and tested). Looks good so far.
Additional change:
In my entity container TT, I had to modify the definition of the EscapeEndTypeName to the following:
string EscapeEndTypeName(AssociationType association, int index,
CodeGenerationTools code)
{
EntityType entity = association.AssociationEndMembers[index]
.GetEntityType();
return code.CreateFullName(
code.EscapeNamespace(association.NamespaceName), code.Escape(entity));
}
I'm using association.NamespaceName as it contains the correct namespace from the other assembly.
I don't know the answer, but I think that your question is essentially equivalent to "Is it possible to cause a T4 template in one project to emit code into a different project?" If you can do that, then you can do what you want. Note, though, that this is substantially easier in EF 4.
So I think you might get useful feedback if you asked that question directly.