title: C#/Xamarin Constructor new Java Object
body: <p>Need to use C#/Xamarin to use a Java library.</p>
  
  <p>Java</p>
  
  <pre><code>class MyObject{
      private final string1;
      private final Date date;
      public MyObject(final String string1, final Date date){
          //init here
      }
  }
  </code></pre>
  
  <p>In the generated .cs file...</p>
  
  <pre><code>namespace Com.MyPackage{
      internal MyObject(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer){}
  
      static IntPtr id_getString1;
      public unsafe string String1{
          [Register ("getString1", "()Ljava/lang/String;", "GetString1Handler")]
  
          get{
              if(id_getString1 == IntPtr.Zero){
                  id_getString1 = JNIEnv.GetMethodID(class_ref, "getString1", "()Ljava/lang/String;");
                  try{
                      return JNIEnv.GetString(JNIEnv.CallObjectMethod(((global::Java.Lang.Object) this).Handle, id_getString1), JniHandleOwnership.TransferLocalRef);
                  }finally{
                  }
              }
          }
      }
  
      //...
  }
  </code></pre>
  
  <p>OK, so it is internal. So I make a new method and changed it to this:</p>
  
  <pre><code>namespace Com.MyPackage{
      public MyObject(Java.Lang.String string1, Java.Util.Date dateIntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer){
          String1 = string1; //error
      }
  
      static IntPtr id_getString1;
      public unsafe string String1{
          [Register ("getString1", "()Ljava/lang/String;", "GetString1Handler")]
  
          get{
              if(id_getString1 == IntPtr.Zero){
                  id_getString1 = JNIEnv.GetMethodID(class_ref, "getString1", "()Ljava/lang/String;");
                  try{
                      return JNIEnv.GetString(JNIEnv.CallObjectMethod(((global::Java.Lang.Object) this).Handle, id_getString1), JniHandleOwnership.TransferLocalRef);
                  }finally{
                  }
              }
          }
      }
  
      //...
  }
  </code></pre>
  
  <p>This causes the error</p>
  
  <blockquote>
    <p>Property or indexer 'MyObject.String1' cannot be assigned to -- it is read only</p>
  </blockquote>
  
  <p>This is how I am trying to get a new instance in C#.</p>
  
  <pre><code>IntPtr Map_Class = JNIEnv.FindClass("com/myPackage/MyObject");
  IntPtr Map_defCtor = JNIEnv.GetMethodID(Map_Class, "&lt;init&gt;", "(Java.Lang.String, Java.Util.Date)V");
  IntPtr ptr = JNIEnv.NewObject(Map_Class, Map_defCtor);
  MyObject myObject = new MyObject("hello world", new Java.Util.Date(123),
          ptr, JniHandleOwnership.TransferLocalRef
  ).JavaCast&lt;MyObject&gt;();
  </code></pre>