reflection

Creating an instance of variable with reflection

时光毁灭记忆、已成空白 提交于 2020-01-16 18:02:06
问题 I want to create an instance of type t with reflection, that is Type t = typeof(string); string s = (t)Activator.CreateInstance(t); // this fails because of convertion string s = Activator.CreateInstance(t) as t // also fails Is there a way to perform such a convertion? Thanks. 回答1: Yes. You have to convert to string , not to t . You may want a generic method, alternatively: public T GetInstance<T>() { Type t = typeof(T); T s = (T)Activator.CreateIstance(t); return s; } As things stand you

sharing java objects between applications

故事扮演 提交于 2020-01-16 08:49:14
问题 I want to write an application that when deployed on a server discovers some classes on that server and their methods and exposé a list of it to a client (via web service, servlet or RMI). the client can then choose a method and activate it (with reflection). Problem is that my application is not in the same context of the other applications on the server so I can't activate their methods (I don't have access to their classloader). I have a few ideas but not sure if they are feasible: Create

Using Scala reflection in Scala macros

孤人 提交于 2020-01-15 11:26:12
问题 I'm trying to use Scala macros in order to generate some code, more specifically I would like the macro to generate a function that instantiates an abstract class. For example, if this is the abstract class: abstract class Person { val name: String val age: Int } So the macro will be something like this: def m = macro _m def _m(c: Context): c.Expr[Any] = { import c.universe._ c.Expr(c.parse(""" (_name:String, _age:Int) => new Person{ val name = _name val age = _age } """)) } So far so good,

How many nanoseconds does a call to GetProperties() take?

ぐ巨炮叔叔 提交于 2020-01-15 11:25:09
问题 In relation to: Big-O of .GetProperties() How many nanoseconds does the .GetProperties() method take in C#? EDIT Tested: On simple dev computer (nothing fancy), item has 8 properties, and no inheritance: stopwatch.Start(); for (int i = 0; i < 10000; i++) { PropertyInfo[] properties = item.GetType().GetProperties(); } stopwatch.Stop(); Results: Total Time in Nanoseconds: 16,569.8 Total Time in Milliseconds: 16.5698 Average Time Per .GetProperties() Call: 1.65 ns (This is an assumption, not

How many nanoseconds does a call to GetProperties() take?

放肆的年华 提交于 2020-01-15 11:25:06
问题 In relation to: Big-O of .GetProperties() How many nanoseconds does the .GetProperties() method take in C#? EDIT Tested: On simple dev computer (nothing fancy), item has 8 properties, and no inheritance: stopwatch.Start(); for (int i = 0; i < 10000; i++) { PropertyInfo[] properties = item.GetType().GetProperties(); } stopwatch.Stop(); Results: Total Time in Nanoseconds: 16,569.8 Total Time in Milliseconds: 16.5698 Average Time Per .GetProperties() Call: 1.65 ns (This is an assumption, not

Java - An interface that has static field pointing to class name of its sub class?

别说谁变了你拦得住时间么 提交于 2020-01-15 11:08:49
问题 I want an interface, which its sub class can inherit a static field, that field points to the name of the sub class. How can I do that? For example in my mind (the code is unusable): public interface ILogger<A> { public static String LogTag = A.class.getName(); } public class Sub implements ILogger<Sub> { public Sub() { Log.debug(LogTag, ...); } } 回答1: In Java, unlike C++, this is not possible due to the way that generics are implemented. In Java, there is only one class for each generic type

Instantiating classes stored in metatype Dictionary

佐手、 提交于 2020-01-15 10:17:12
问题 I've followed the solution at Make a Swift dictionary where the key is "Type"? to create dictionaries that can use a class type as keys. What I want to do is: I have one dictionary that should store class types with their class type (aka metatype) as keys, too: class MyScenario { static var metatype:Metatype<MyScenario> { return Metatype(self) } } var scenarioClasses:[Metatype<MyScenario>: MyScenario.Type] = [:] Then I have methods to register and execute scenarios: public func

Android how to detect if outgoing call is answered

╄→гoц情女王★ 提交于 2020-01-15 10:09:39
问题 I'm developing an app that will only be used in house for testing purpose. I have searched a lot and tried different suggestions as suggested in different post but none seems to be working for me. I'm open to any suggestions like Reflections, Accessibility Service, root or any other hack. Please help. Regards 回答1: TRY THIS Set all required permission in manifest.xml file. Call this class in Service public class PhoneListener extends PhoneStateListener { private static PhoneListener instance =

Android how to detect if outgoing call is answered

好久不见. 提交于 2020-01-15 10:09:20
问题 I'm developing an app that will only be used in house for testing purpose. I have searched a lot and tried different suggestions as suggested in different post but none seems to be working for me. I'm open to any suggestions like Reflections, Accessibility Service, root or any other hack. Please help. Regards 回答1: TRY THIS Set all required permission in manifest.xml file. Call this class in Service public class PhoneListener extends PhoneStateListener { private static PhoneListener instance =

Error binding to target method

大兔子大兔子 提交于 2020-01-15 09:13:33
问题 I am trying to call a static method from a helper class, of which the type is not known until runtime. I thought I had solved the problem however I am getting the following error - "Error binding to target method." Can anyone see what is wrong with this code? Any help would be appreciated.. Delegate del = Delegate.CreateDelegate(typeof(Func<string>), typeof(RepositoryStringExtensions).GetMethod("GetTableName", BindingFlags.Static | BindingFlags.Public) .MakeGenericMethod(new Type[] {