Alexey Subbota
Alexey Subbota

Reputation: 972

Using java class in Xamarin

I have a Xamarin.Android project. How can I use java class declared in java file from C# code? How can I reference to it?

That is what I did: 1) I've added java file to project, 2) set up build action to AndroidJavaSource 3) Compiled (and seen Foo.class in obj folder)

Foo.java

package x.y.z;
public class Foo{
}

How can I reference to Foo from C#?

var f = new Foo();
f.someFunction();

Upvotes: 1

Views: 885

Answers (1)

Alexey Subbota
Alexey Subbota

Reputation: 972

I've solved it!

Instance is created by

Java.Lang.Object obj = Java.Lang.Class.ForName("x.y.z.Foo").NewInstance();

You can cast Java.Lang.Object to a known ancestor if necessary.

Upvotes: 1

Related Questions