简体   繁体   English

单声道android的Java反射

[英]Java reflection for mono android

I was facing WRAP_CONTENT not work on a RecyclerView , so I googled it and found the workaround. 我面临WRAP_CONTENT无法在RecyclerView上工作 ,所以我用Google搜索并找到了解决方法。 When I tried to implement the workaround in c#, I was stuck at this line: 当我尝试在c#中实现变通方法时,我被困在这一行:

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

Here I tried to port the original java code ,and ported code avilable in gist 在这里,我尝试移植原始的Java代码 ,并在gist中移植代码

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: Java中的.class “keyword”等同于C#中的typeof()关键字,所以这是(kinda / sorta),如:

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. 除非它不是,因为typeof()返回一个System.Type ,它对java.lang.Object实例一无所知。

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: 相反,您应该使用Java.Lang.Class.FromType(Type)来获取Java.Lang.Class实例,然后允许您使用Java Reflection:

var klass = Java.Lang.Class.FromType (typeof (RecyclerView.LayoutParams));
var insetsDirtyField = klass.GetDeclaredField("mInsetsDirty");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM