reflection

How to get the right selector from URLSession.dataTask(with:completionHandler:) on Xcode 11.5?

那年仲夏 提交于 2021-01-29 10:01:11
问题 On Xcode 11.5 (Swift 5.2.4) the compiler gets confused and shows the error below: Cannot convert value of type '(URLSession) -> (URL, @escaping (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask' to type '(URLSession) -> (URLRequest, @escaping (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask' in coercion I'm trying to get the selector like follows, but the compiler still tries to convert to the version with URL instead: let selector = #selector(URLSession.dataTask(with

Type erasure problem in method overloading

走远了吗. 提交于 2021-01-29 09:04:27
问题 I have two overloaded method having following signatures - def fun(x: Seq[String]): Future[Seq[Int]] = ??? def fun(x: Seq[(String, String)]): Future[Seq[Int]] = ??? Due to type erasure, these methods can't be overloaded and hence showing compilation error. I tried using typetags as a workaround - def fun[t: TypeTag](values: Seq[T]): Future[Seq[Int]] = { typeOf[T] match { case t if t =:= typeOf[String] => ??? case t if t =:= typeOf[(String, String)] => ??? case _ => ??? // Should not come here

Getting a Property list with similar name for looping c#

江枫思渺然 提交于 2021-01-29 07:43:45
问题 I have a class with several properties public class Hotel { public string ID {get; set;} public string Name {get; set;} public string Location {get; set;} public Image Image1 {get; set;} public Image Image2 {get; set;} public Image Image3 {get; set;} public Image Image4 {get; set;} } I want to get a list of all the properties starting with property name "Image" ex public List<Image> GetImageList(Hotel hotel) { List<Image> imageList = new List<Image>(); foreach(var item in <ImageList>) { rest

How to reflect concrete return types for methods of classes defined at runtime using the Scala ToolBox?

别等时光非礼了梦想. 提交于 2021-01-29 07:07:43
问题 When reflecting the foo() method of the class Cls we can easily get the concrete return type using the following. class Cls { def foo() = List("A", "B") } val classType = ru.typeOf[Cls] val classMirror = toolbox.mirror.reflectClass(classType.typeSymbol.asClass) val ctorSymbol = classType.decl(ru.termNames.CONSTRUCTOR).asMethod val methodSymb = classType.decl(ru.TermName("foo")).asMethod val ctor = classMirror.reflectConstructor(ctorSymbol) val instance = ctor() val im = toolbox.mirror.reflect

Does using System.Reflection.MethodBase.GetCurrentMethod() in a method prevent the JIT compiler from inlining the method?

时光毁灭记忆、已成空白 提交于 2021-01-29 03:29:22
问题 The answers and comments in the following questions provide conflicting information. How to get the name of the current method from code Can you use reflection to find the name of the currently executing method? It is not mentioned in the documentation as well. https://msdn.microsoft.com/en-us/library/system.reflection.methodbase.getcurrentmethod.aspx 回答1: No, the JIT compiler is free to inline the method. You'll need to add the [MethodImpl(MethodImplOptions.NoInlining)] to any method that

How to invoke default constructor using scala reflection if we have default value defined in our constructor

你。 提交于 2021-01-28 21:15:00
问题 class Person(name: String = "noname", age:Int = -1){} object ReflectionTester{ def main(args: Array[String]) { val m = ru.runtimeMirror(getClass.getClassLoader) val classTest = m.staticClass("Person") val theType = classTest.toType //class mirror val cm = m.reflectClass(classTest) val ctor = theType.decl(ru.termNames.CONSTRUCTOR).asMethod } } Invoking class mirror gives me the default constructor of passing name and age but there are inferred constructor too right where i can create a person

Reflection, Get return value from a method

和自甴很熟 提交于 2021-01-28 19:38:44
问题 How can we exacute a method and get the return value from Reflection. Type serviceType = Type.GetType("class", true); var service = Activator.CreateInstance(serviceType); serviceType.InvokeMember("GetAll", BindingFlags.InvokeMethod, Type.DefaultBinder, service, null); 回答1: cast the InvokeMember result to the type actually returned by the method call. 回答2: I am not sure whether you are interested on the return value or the return Type. Well both are answered by the code below, where I try to

Get compiler generated delegate for an event

别来无恙 提交于 2021-01-28 14:49:48
问题 I need to know what handlers are subsribed to the CollectionChanged event of the ObservableCollection class. The only solution I found would be to use Delegate.GetInvocationList() on the delegate of the event. The problem is, I can't get Reflection to find the compiler generated delegate. AFAIK the delegate has the same name as the event. I used the following piece of code: PropertyInfo notifyCollectionChangedDelegate = collection.GetType().GetProperty("CollectionChanged", BindingFlags

How to dynamically add all methods of a class into another class

僤鯓⒐⒋嵵緔 提交于 2021-01-28 14:22:51
问题 I have a global shared library on Jenkins implicitly loaded on all pipelines, then my Jenkinsfile is like that: new com.company.Pipeline()() And then the shared library has on directory src/com/company some files, below the Pipeline.groovy class: package com.company import static Utils.* def call() { // some stuff here... } The problem is, this way I have to static declare all methods, thus I lose the context and cannot access jenkins' methods easly without the Pipeline class' instance. As

Create Delegate from MethodInfo with Output Parameters, without Dynamic Invoke

孤街醉人 提交于 2021-01-28 12:00:59
问题 I am trying to get a Delegate from a MethodInfo object that has Output Parameters. My code follows: static void Main(string[] args) { MethodInfo m = typeof(Program).GetMethod("MyMethod2"); IEnumerable<Type> paramsTypes = m.GetParameters().Select(p => p.ParameterType); Type methodType = Expression.GetDelegateType(paramsTypes.Append(m.ReturnType).ToArray()); Delegate d = m.CreateDelegate(methodType); Action a = (Action)d; a(); } I'm getting is a System.InvalidCastException: Unable to cast