reflection

Dynamic compilation of multiple Scala classes at runtime

依然范特西╮ 提交于 2020-12-26 01:58:54
问题 I know I can compile individual "snippets" in Scala using the Toolbox like this: import scala.reflect.runtime.universe import scala.tools.reflect.ToolBox object Compiler { val tb = universe.runtimeMirror(getClass.getClassLoader).mkToolBox() def main(args: Array[String]): Unit = { tb.eval(tb.parse("""println("hello!")""")) } } Is there any way I can compile more than just "snippets", i.e., classes that refer to each other? Like this: import scala.reflect.runtime.universe import scala.tools

Dynamic compilation of multiple Scala classes at runtime

為{幸葍}努か 提交于 2020-12-26 01:57:27
问题 I know I can compile individual "snippets" in Scala using the Toolbox like this: import scala.reflect.runtime.universe import scala.tools.reflect.ToolBox object Compiler { val tb = universe.runtimeMirror(getClass.getClassLoader).mkToolBox() def main(args: Array[String]): Unit = { tb.eval(tb.parse("""println("hello!")""")) } } Is there any way I can compile more than just "snippets", i.e., classes that refer to each other? Like this: import scala.reflect.runtime.universe import scala.tools

Dynamic compilation of multiple Scala classes at runtime

醉酒当歌 提交于 2020-12-26 01:57:06
问题 I know I can compile individual "snippets" in Scala using the Toolbox like this: import scala.reflect.runtime.universe import scala.tools.reflect.ToolBox object Compiler { val tb = universe.runtimeMirror(getClass.getClassLoader).mkToolBox() def main(args: Array[String]): Unit = { tb.eval(tb.parse("""println("hello!")""")) } } Is there any way I can compile more than just "snippets", i.e., classes that refer to each other? Like this: import scala.reflect.runtime.universe import scala.tools

Loop over values in an IEnumerable<> using reflection

大憨熊 提交于 2020-12-25 01:39:28
问题 Given an object possibly containing an IEnumerable<T> , how would I check that an IEnumerable<T> property exists, and if it does, loop over all values in that IEnumerable<T> using reflection, for any T ? 回答1: foreach (var property in yourObject.GetType().GetProperties()) { if (property.PropertyType.GetInterfaces().Contains(typeof(IEnumerable))) { foreach (var item in (IEnumerable)property.GetValue(yourObject, null)) { //do stuff } } } 回答2: Well, you can test it as Aghilas said and, once

Loop over values in an IEnumerable<> using reflection

。_饼干妹妹 提交于 2020-12-25 01:37:39
问题 Given an object possibly containing an IEnumerable<T> , how would I check that an IEnumerable<T> property exists, and if it does, loop over all values in that IEnumerable<T> using reflection, for any T ? 回答1: foreach (var property in yourObject.GetType().GetProperties()) { if (property.PropertyType.GetInterfaces().Contains(typeof(IEnumerable))) { foreach (var item in (IEnumerable)property.GetValue(yourObject, null)) { //do stuff } } } 回答2: Well, you can test it as Aghilas said and, once

How to implement databinding with Reflection in BaseFragment()

偶尔善良 提交于 2020-12-15 06:33:29
问题 I'm trying to implement a BaseFragment in which I will pass the layout resource on it and it should outputs the binding to work in the fragment itself instead of need to do it everytime the fragment is extended. For example I have this BaseFragment open class BaseFragment(@LayoutRes contentLayoutId : Int = 0) : Fragment(contentLayoutId) { private lateinit var onInteractionListener: OnFragmentInteractionListener val toolbar : Toolbar? get() { return if(activity is BaseActivity) (activity as

How to implement databinding with Reflection in BaseFragment()

∥☆過路亽.° 提交于 2020-12-15 06:32:15
问题 I'm trying to implement a BaseFragment in which I will pass the layout resource on it and it should outputs the binding to work in the fragment itself instead of need to do it everytime the fragment is extended. For example I have this BaseFragment open class BaseFragment(@LayoutRes contentLayoutId : Int = 0) : Fragment(contentLayoutId) { private lateinit var onInteractionListener: OnFragmentInteractionListener val toolbar : Toolbar? get() { return if(activity is BaseActivity) (activity as

How to implement databinding with Reflection in BaseFragment()

為{幸葍}努か 提交于 2020-12-15 06:31:31
问题 I'm trying to implement a BaseFragment in which I will pass the layout resource on it and it should outputs the binding to work in the fragment itself instead of need to do it everytime the fragment is extended. For example I have this BaseFragment open class BaseFragment(@LayoutRes contentLayoutId : Int = 0) : Fragment(contentLayoutId) { private lateinit var onInteractionListener: OnFragmentInteractionListener val toolbar : Toolbar? get() { return if(activity is BaseActivity) (activity as

How to find class parameter datatype at runtime in scala

断了今生、忘了曾经 提交于 2020-12-15 05:26:12
问题 import scala.reflect.runtime.universe import scala.reflect.runtime.universe._ def getType[T: TypeTag](obj: T) = typeOf[T] case class Thing( val id: Int, var name: String ) val thing = Thing(1, "Apple") val dataType = getType(thing).decl(TermName("id")).asTerm.typeSignature dataType match { case t if t =:= typeOf[Int] => println("I am Int") case t if t =:= typeOf[String] => println("String, Do some stuff") case _ => println("Absurd") } Not able to digest why result is Absurd instead of I am

How do I get the Reflection TypeInfo of a C# 9 program that use Top-level statements?

∥☆過路亽.° 提交于 2020-12-12 05:39:42
问题 Assume I have a simple script writing in C# 9 like this: using System; using System.IO; // What to put in the ??? var exeFolder = Path.GetDirectoryName(typeof(???).Assembly.Location); Before, with the full program, we can use the Main class as an "indicator" class. this and this.GetType() is not available because technically it's inside a static method. How do I get it now? A workaround I thought of while typing the question is Assembly.GetCallingAssembly() : var exeFolder = Path