CSharp/C# >> compiling C# to Java byte code

by Bootstrap Bill » Mon, 09 Feb 2004 17:59:51 GMT

I heard there is an add on for Visual Studio .net that will allow C# to
create Java byte code. Does anyone have the URL for the project?





CSharp/C# >> compiling C# to Java byte code

by Jon Skeet [C# MVP] » Mon, 09 Feb 2004 18:30:23 GMT



I haven't heard of any such thing, and I'd be surprised if it existed,
given that C# doesn't have checked exceptions, and *does* have
properties etc. It would have to be extremely limited, at least.

There *is* a Java to C# conversion tool in VS.NET 2003, however.

--
Jon Skeet - < XXXX@XXXXX.COM >
http://www.pobox.com/ ~skeet
If replying to the group, please do not mail me too

CSharp/C# >> compiling C# to Java byte code

by Shawn B. » Tue, 10 Feb 2004 00:58:49 GMT

C# properties compile to a get_PropertyName... set_PropertyName internally.
So it wouldn't be too hard to make such a utility.

Thanks,
Shawn

CSharp/C# >> compiling C# to Java byte code

by Jon Skeet [C# MVP] » Tue, 10 Feb 2004 01:11:12 GMT


True. Properties was a bad choice. Delegates and events are better
examples. You *could* possibly do something with interfaces, but it
would be truly foul (and you'd have to do something interesting for
delegates which ended up being static methods).


Well, you'd need to know what to do with *all* the .NET types used by
the program. Even the J# converter doesn't do a brilliant job, and Java
1.1 was tiny compared with the .NET framework. Even if theoretically
possible, it would be nightmarishly difficult for a very small
audience, IMO.

--
Jon Skeet - < XXXX@XXXXX.COM >
http://www.pobox.com/ ~skeet
If replying to the group, please do not mail me too

Similar Threads

1. Compiling Java code within a C# made program,,,,, - CSharp/C#

2. [OT] C to Java Byte Code

Galga wrote:

> The product says it will compile into Java Byte Code, Not, I repeat,
> NOT, Java Source; Those are two COMPLETELY seperate tasks. Get it right
> or shut up Paul, because you're jsut diggering your own usenet grave
> deeper and deeper.

I must point out that Java bytecodes *that pass the bytecode verifier* 
can almost always be back-translated to Java methods, and that the 
exceptions (that I know of) are trivial [1] rather than interesting.

So, while Paul may be wrong, I don't think it's for that reason.

[1] I don't think the verifier complains about unreachable code, nor
    about code-not-involving-this before the superconstructor gets
    called; directly expressed in Java these provoke diagnostics. 

-- 
Chris "electric hedgehog" Dollin

3. C to Java Byte Code

4. Troll Alert: C to Java Byte Code

Mark McIntyre wrote:

> Paul Lutus wrote:
> 
>>Yes, it is a union in C, 
>>but I am saying it is not a union in the Java bytecode. 
> 
> Marvellous. Since this has nothing to do with C any more, 
> why the heck are you stupidly crossposting this long and pointless thread 
> in CLC and CLC++.
> Go away. 

Why are you responding to a post
that you obviously think is a troll?
Honestly,
I don't see any difference between you and Paul Lutus
and I don't know which one of you is worse
I think that you are both trolls
and I'll bet that most other subscribers think so too.

5. Can't convert java Byte to c# byte through web reference

6. using C# code in C++ or Java - C# inteop

Dear all,

I need a little help here. I have written some code written in C#. I compiled this as an executable.
I want to call this application from within Java. This can be achieved by using Runtime.exec(...) methods.
This worked fine until i wanted some bidirectional communication between the java app and the c# app.
I don't want to use sockets, soap or corba. But i looked at the JNI. I experimented with it for some time
and it seems to work fine for me. However i have only communication between Java and VC++.
My problem can be solved by either a bridge between java and c# or the knowledge how to use the c# app
in VC++. For the former i have never seen any example of this. For the latter, i only need to know how
I can make an instance of a class I created in C#. How do I call the constructor, how do i invoke methods
on that object...
Any help or pointers are welcome

thanx in advance,
srtnxalt

7. Compiling Java code within a C#Builder... - CSharp/C#

8. translating from C# to vb.net (event handler code) - "Form1.cs" 6528 Bytes

HI Michael,

I guess you might need to use GetType(EventHandler) for the first parameter
in CreateDelegate() call in your VB.NET version of the code.
Try replacing "form.GetType"with "GetType(EventHandler)" to see if it helps.

Regards,
Aravind C


"Michael" < XXXX@XXXXX.COM > wrote in message
news:YaxDb.222077$ XXXX@XXXXX.COM ...
> I have some sample code that works for a UI Test Automation program for a
> simple c# form.
> I am trying to translate this (Port it) to vb.net.
>
> The function I am having problems translating is the InvokeMethod
function,
> mainly due to a difference in syntax for delegates and Handlers between C#
> and VB.net.
> I haven't really mastered these concepts yet.
>
> The C# Code is as follows:
> private void InvokeMethod(Form form, string methodName,
> params object[] parms)
> {
> EventHandler eh = (EventHandler)Delegate.CreateDelegate(typeof
> (EventHandler), form, methodName);
> if (eh != null)
> {
> form.Invoke(eh, parms);
> }
> }
>
> The vb.net code that I have attempted to port from the above is as
follows:
>     Private Sub InvokeMethod(ByVal form As Form, ByVal methodName As
String,
>     ByVal params As Object())
>         Dim eh As EventHandler = EventHandler.CreateDelegate(form.GetType,
>        form, methodName)
>         If Not IsNothing(eh) Then
>             form.Invoke(eh, params)
>         End If
>     End Sub
>
> When I run the code that invokes a method from the sample external
> application, I get the following error:
>
> An unhandled exception of type 'System.ArgumentException' occurred in
> mscorlib.dll
>
> Additional information: Type must derive from Delegate.
>
> If I trap the System.ArgumentException error, I get a full error message
of:
> System.ArgumentException: Type must derive from Delegate.
> Parameter name: type
>    at System.Delegate.CreateDelegate(Type type, Object target, String
>    method)
>    at MyWinAppTester.Form1.InvokeMethod(Form form, String methodName,
>    Object[] params) in E:\Documents\Visual Studio
>    Projects\VB.NET\MyWinAppTester\MyWinAppTester\Form1.vb:line 111
>
> Now, that being said...If this isn't enough info, here is the source for
> both projects.
>
> Any clues on how to modify my vb.net code to accomplish this task would be
> greatly appreciated.
>
> Thanks in advance.
>
> Sincerely,
>
> Michael
> C#
> using System;
> using System.Drawing;
> using System.Collections;
> using System.ComponentModel;
> using System.Windows.Forms;
> using System.Data;
>
> using System.Reflection;
> using System.Threading;
>
> namespace MyWinAppTester
> {
> /// <summary>
> /// Summary description for Form1.
> /// </summary>
> public class Form1 : System.Windows.Forms.Form
> {
> private System.Windows.Forms.TextBox textBox1;
> private System.Windows.Forms.TextBox textBox2;
> private System.Windows.Forms.Button btnLaunchApp;
> private System.Windows.Forms.Button btnInvokeMethod;
> private System.Windows.Forms.Button btnRunTest;
> /// <summary>
> /// Required designer variable.
> /// </summary>
> private System.ComponentModel.Container components = null;
>
> public Form1()
> {
> //
> // Required for Windows Form Designer support
> //
> InitializeComponent();
>
> //
> // TODO: Add any constructor code after InitializeComponent call
> //
> }
>
> /// <summary>
> /// Clean up any resources being used.
> /// </summary>
> protected override void Dispose( bool disposing )
> {
> if( disposing )
> {
> if (components != null)
> {
> components.Dispose();
> }
> }
> base.Dispose( disposing );
> }
>
> #region Windows Form Designer generated code
> /// <summary>
> /// Required method for Designer support - do not modify
> /// the contents of this method with the code editor.
> /// </summary>
> private void InitializeComponent()
> {
> this.btnLaunchApp = new System.Windows.Forms.Button();
> this.btnInvokeMethod = new System.Windows.Forms.Button();
> this.btnRunTest = new System.Windows.Forms.Button();
> this.textBox1 = new System.Windows.Forms.TextBox();
> this.textBox2 = new System.Windows.Forms.TextBox();
> this.SuspendLayout();
> //
> // btnLaunchApp
> //
> this.btnLaunchApp.Location = new System.Drawing.Point(16, 16);
> this.btnLaunchApp.Name = "btnLaunchApp";
> this.btnLaunchApp.TabIndex = 0;
> this.btnLaunchApp.Text = "Launch App";
> this.btnLaunchApp.Click += new System.EventHandler(this.LaunchApp_Click);
> //
> // btnInvokeMethod
> //
> this.btnInvokeMethod.Location = new System.Drawing.Point(16, 168);
> this.btnInvokeMethod.Name = "btnInvokeMethod";
> this.btnInvokeMethod.Size = new System.Drawing.Size(96, 23);
> this.btnInvokeMethod.TabIndex = 1;
> this.btnInvokeMethod.Text = "Invoke Method";
> this.btnInvokeMethod.Click += new
> System.EventHandler(this.InvokeMethod_Click);
> //
> // btnRunTest
> //
> this.btnRunTest.Location = new System.Drawing.Point(16, 200);
> this.btnRunTest.Name = "btnRunTest";
> this.btnRunTest.Size = new System.Drawing.Size(96, 23);
> this.btnRunTest.TabIndex = 2;
> this.btnRunTest.Text = "Run Test";
> this.btnRunTest.Click += new System.EventHandler(this.RunTest_Click);
> //
> // textBox1
> //
> this.textBox1.Location = new System.Drawing.Point(120, 168);
> this.textBox1.Name = "textBox1";
> this.textBox1.TabIndex = 3;
> this.textBox1.Text = "";
> //
> // textBox2
> //
> this.textBox2.Location = new System.Drawing.Point(120, 200);
> this.textBox2.Name = "textBox2";
> this.textBox2.TabIndex = 4;
> this.textBox2.Text = "";
> //
> // Form1
> //
> this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
> this.ClientSize = new System.Drawing.Size(292, 266);
> this.Controls.Add(this.textBox2);
> this.Controls.Add(this.textBox1);
> this.Controls.Add(this.btnRunTest);
> this.Controls.Add(this.btnInvokeMethod);
> this.Controls.Add(this.btnLaunchApp);
> this.Name = "Form1";
> this.Text = "Form1";
> this.ResumeLayout(false);
>
> }
> #endregion
>
> /// <summary>
> /// The main entry point for the application.
> /// </summary>
> [STAThread]
> static void Main()
> {
> Application.Run(new Form1());
> }
> Assembly testAssembly = null;
> Form testForm = null;
> BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic |
> BindingFlags.Static | BindingFlags.Instance;
>
> static void RunApp(object state) // Need this function to pass into
> // WaitCallBack().
> {
> Application.Run((Form)state);
> }
>
> private void InvokeMethod(Form form, string methodName,
> params object[] parms)
> {
> EventHandler eh = (EventHandler)Delegate.CreateDelegate(typeof
> (EventHandler), form, methodName);
> if (eh != null)
> {
> form.Invoke(eh, parms);
> }
> }
>
> private object GetField(object obj, string fieldName)
> {
> Type t = obj.GetType();
> FieldInfo fi = t.GetField(fieldName, flags);
> return fi.GetValue(obj);
> }
>
> private object GetProperty(object obj, string propertyName)
> {
> Type t = obj.GetType();
> PropertyInfo pi = t.GetProperty(propertyName, flags);
> return pi.GetValue(obj, new object[0]);
> }
>
> // Launch MyWinApp
> private void LaunchApp_Click(object sender, System.EventArgs e)
> {
> testAssembly = Assembly.LoadFrom("E:\\Documents\\Visual Studio
> Projects\\C#\\MyWinApp\\MyWinApp\\bin\\Release\\MyWinApp.exe");
> Type t = testAssembly.GetType("MyWinApp.Form1");
> testForm = (Form)testAssembly.CreateInstance(t.FullName);
> testForm.Show();
> ThreadPool.QueueUserWorkItem(new WaitCallback(RunApp), testForm);
>
> }
> // Invoke Method
> private void InvokeMethod_Click(object sender, System.EventArgs e)
> {
> object[] p = {this, new EventArgs()};
> string meth = this.textBox1.Text.ToString();
> InvokeMethod(testForm, meth, p);
>
> }
>
> private void RunTest_Click(object sender, System.EventArgs e)
> {
> object[] p = {this, new EventArgs()}; // arguments for a button click
> bool pass = true;
> for (int count = 1; count <= 3; ++count)
> {
> InvokeMethod(testForm, "button1_Click", p);
> Thread.Sleep(1000);
> if ( (string)(GetProperty(GetField(testForm,"textBox1"), "Text"))
> != "Hello World")
> pass = false;
>
> ListBox.ObjectCollection lboc = (ListBox.ObjectCollection)
> GetProperty(GetField(testForm, "listBox1"), "Items");
> if (lboc.Contains("Goodbye World") )
> pass = false;
>
> InvokeMethod(testForm, "button2_Click", p);
> Thread.Sleep(1000);
> // check that textBox1 has "Hello World" & listBox1 has
> // "Goodbye World"
>
> InvokeMethod(testForm, "button3_Click", p);
> Thread.Sleep(1000);
> // check to make sure that textBox1 and listBox1 are both empty
> }
>
> this.textBox2.Text = (pass) ? "Pass" : "Fail"; // display test result
>
>
> }
>
>
>
>
> }
> }
> --------------------------------------------------------------------------
-----------
> vb.net
> Imports System.Reflection
> Imports System.Threading
> Public Class Form1
>     Inherits System.Windows.Forms.Form
>
> #Region " Windows Form Designer generated code "
>
>     Public Sub New()
>         MyBase.New()
>
>         'This call is required by the Windows Form Designer.
>         InitializeComponent()
>
>         'Add any initialization after the InitializeComponent() call
>
>     End Sub
>
>     'Form overrides dispose to clean up the component list.
>     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
>         If disposing Then
>             If Not (components Is Nothing) Then
>                 components.Dispose()
>             End If
>         End If
>         MyBase.Dispose(disposing)
>     End Sub
>
>     'Required by the Windows Form Designer
>     Private components As System.ComponentModel.IContainer
>
>     'NOTE: The following procedure is required by the Windows Form
Designer
>     'It can be modified using the Windows Form Designer.
>     'Do not modify it using the code editor.
>     Friend WithEvents Button1 As System.Windows.Forms.Button
>     Friend WithEvents Button2 As System.Windows.Forms.Button
>     Friend WithEvents Button3 As System.Windows.Forms.Button
>     Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
>     Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
>     <System.Diagnostics.DebuggerStepThrough()> Private Sub
>     InitializeComponent()
>         Me.Button1 = New System.Windows.Forms.Button
>         Me.Button2 = New System.Windows.Forms.Button
>         Me.Button3 = New System.Windows.Forms.Button
>         Me.TextBox1 = New System.Windows.Forms.TextBox
>         Me.TextBox2 = New System.Windows.Forms.TextBox
>         Me.SuspendLayout()
>         '
>         'Button1
>         '
>         Me.Button1.Location = New System.Drawing.Point(16, 8)
>         Me.Button1.Name = "Button1"
>         Me.Button1.Size = New System.Drawing.Size(88, 23)
>         Me.Button1.TabIndex = 0
>         Me.Button1.Text = "Launch App"
>         '
>         'Button2
>         '
>         Me.Button2.Location = New System.Drawing.Point(16, 40)
>         Me.Button2.Name = "Button2"
>         Me.Button2.Size = New System.Drawing.Size(88, 23)
>         Me.Button2.TabIndex = 1
>         Me.Button2.Text = "Invoke Method"
>         '
>         'Button3
>         '
>         Me.Button3.Location = New System.Drawing.Point(16, 80)
>         Me.Button3.Name = "Button3"
>         Me.Button3.Size = New System.Drawing.Size(88, 23)
>         Me.Button3.TabIndex = 2
>         Me.Button3.Text = "Run Test"
>         '
>         'TextBox1
>         '
>         Me.TextBox1.Location = New System.Drawing.Point(112, 40)
>         Me.TextBox1.Name = "TextBox1"
>         Me.TextBox1.TabIndex = 3
>         Me.TextBox1.Text = "<method name>"
>         '
>         'TextBox2
>         '
>         Me.TextBox2.Location = New System.Drawing.Point(112, 80)
>         Me.TextBox2.Name = "TextBox2"
>         Me.TextBox2.TabIndex = 4
>         Me.TextBox2.Text = "<test results>"
>         '
>         'Form1
>         '
>         Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
>         Me.ClientSize = New System.Drawing.Size(292, 266)
>         Me.Controls.Add(Me.TextBox2)
>         Me.Controls.Add(Me.TextBox1)
>         Me.Controls.Add(Me.Button3)
>         Me.Controls.Add(Me.Button2)
>         Me.Controls.Add(Me.Button1)
>         Me.Name = "Form1"
>         Me.Text = "Form1"
>         Me.ResumeLayout(False)
>
>     End Sub
>
> #End Region
>
>     Dim testAssembly As [Assembly] = Nothing
>     Dim testForm As Form
>     Dim flags As BindingFlags = BindingFlags.Public Or BindingFlags.Static
>     Or _
>                                 BindingFlags.NonPublic Or
>        BindingFlags.Instance
>     Sub runApp(ByVal state As Object)
>         Application.Run(CType(state, Form))
>     End Sub
>     Private Sub InvokeMethod(ByVal form As Form, ByVal methodName As
String,
>     ByVal params As Object())
>         Try
>             Dim eh As EventHandler =
>        EventHandler.CreateDelegate(form.GetType, form, methodName)
>             If Not IsNothing(eh) Then
>                 form.Invoke(eh, params)
>             End If
>         Catch ex As System.ArgumentException
>             Console.WriteLine(ex.ToString)
>
>         End Try
>     End Sub
>     Private Function GetField(ByVal obj As Object, ByVal fieldName As
>     String) As Object
>         Dim t As Type = obj.GetType
>         Dim fi As FieldInfo = t.GetField(fieldName, flags)
>         Return fi.GetValue(obj)
>     End Function
>     Private Function GetProperty(ByVal obj As Object, ByVal propertyName
As
>     String) As Object
>         Dim t As Type = obj.GetType
>         Dim pi As PropertyInfo = t.GetProperty(propertyName, flags)
>         Return pi.GetValue(obj, New Object)
>     End Function
>     ' launch app
>     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
>     System.EventArgs) Handles Button1.Click
>         Me.testAssembly = Me.testAssembly.LoadFrom("E:\Documents\Visual
>        Studio Projects\VB.NET\MyWinApp\MyWinApp\bin\MyWinApp.exe")
>         Dim t As Type = testAssembly.GetType("MyWinApp.Form1")
>         testForm = CType(testAssembly.CreateInstance(t.FullName), Form)
>         ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf runApp),
>        testForm)
>     End Sub
>     ' invoke method
>     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
>     System.EventArgs) Handles Button2.Click
>         Dim p As Object() = {Me, New EventArgs}
>         Dim meth As String = Me.TextBox1.Text.ToString
>         Me.InvokeMethod(testForm, meth, p)
>     End Sub
>     ' test code
>     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
>     System.EventArgs) Handles Button3.Click
>         Dim p As Object() = {Me, New EventArgs}
>         Dim pass As Boolean = True
>         Dim i As Integer
>         For i = 0 To 3
>             InvokeMethod(testForm, "button1_click", p)
>             Thread.Sleep(1000)
>             If CType(GetProperty(GetField(testForm, "textbox1"), "Text"),
>        String) <> "Hello World" Then
>                 pass = False
>             End If
>             Dim lboc As ListBox.ObjectCollection =
>        CType(GetProperty(GetField(testForm, "listbox1"), "Items"),
>        ListBox.ObjectCollection)
>             If lboc.Contains("Goodbye World") = False Then
>                 pass = False
>             End If
>             InvokeMethod(testForm, "button2_click", p)
>             Thread.Sleep(1000)
>             ' check that textbox1 has hello world and listbox1 has goodbye
>        world
>             InvokeMethod(testForm, "button3_click", p)
>             Thread.Sleep(1000)
>         Next
>         If pass Then Me.TextBox2.Text = "pass" Else Me.TextBox2.Text =
>        "fail"
>     End Sub
> End Class
>
>
>