Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java reflection for mono android

I was facing WRAP_CONTENT not work on a RecyclerView , so I googled it and found the workaround. When I tried to implement the workaround in c#, I was stuck at this line:

insetsDirtyField = RecyclerView.LayoutParams.class.getDeclaredField("mInsetsDirty");

Here I tried to port the original java code,and ported code avilable in gist

like image 572
Jagadeesh Govindaraj Avatar asked May 26 '26 08:05

Jagadeesh Govindaraj


1 Answers

I was stuck at this line:

insetsDirtyField = RecyclerView.LayoutParams.class.getDeclaredField("mInsetsDirty");

The .class "keyword" in Java is equivalent to the typeof() keyword in C#, so this is (kinda/sorta) like:

var insetsDirtyField = typeof(RecyclerView.LayoutParams).GetDeclaredField("mInsetsDirty");

Except it isn't, because typeof() returns a System.Type, which doesn't know anything about java.lang.Object instances.

Instead, you should use Java.Lang.Class.FromType(Type) to obtain a Java.Lang.Class instance, which will then allow you to use Java Reflection:

var klass = Java.Lang.Class.FromType (typeof (RecyclerView.LayoutParams));
var insetsDirtyField = klass.GetDeclaredField("mInsetsDirty");
like image 186
jonp Avatar answered May 27 '26 22:05

jonp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!