reflection

Store information/reference about structure

大城市里の小女人 提交于 2019-12-31 03:13:13
问题 I am looking for a way to store information which struct a function should use. Each struct corresponds to certain database table. type Record struct { TableName string PrimaryKey string //XormStruct // how can I store User or Post here? XormStruct2 interface{} // see I have tried below XormStruct3 reflect.Type // see I have tried below } var posts []Post var ListOfTables [...]Record { {"User", "id", User}, //{"Post", "post_id", Post}, {"Post", "post_id", posts, reflect.TypeOf([]Post{})}, } /

C# is generic type with generic type constraint

好久不见. 提交于 2019-12-31 02:58:48
问题 Let's asume an interface interface IOwnedBy<T> where T : IOwner { T Owner { get; } } and interface IOwner { public int Id { get; } } Somewhere in my code, I would like to do the following: if (obj is OwnedBy<IOwner>) { DoSomethingWith( obj.Owner.Id ); } Basically, I want to check whether obj is any OwnedBy implementation. As IOwner is the type constraint of any generic parameter, I thought this one would work. But the condition is never met. Any way without using alot of reflection? 回答1:

Java equivalent to python “dir”?

谁说胖子不能爱 提交于 2019-12-31 02:24:27
问题 Is there an equivalent to "dir" in python for java, or a library which provides similar functionality (i.e. properties of objects and classes output as informative strings)? This question is similar this question for clojure and probably has something to do with Java Reflection as in this question, which seems to be about a more complicated, but similar, topic. 回答1: There's nothing in the standard library that accomplishes exactly what dir() does, but you can get the same information using

Unexpected Class.getMethod behaviour

和自甴很熟 提交于 2019-12-31 01:56:10
问题 A while ago I had a similar question when using Class.getMethod and autoboxing, and it made sense to implement this in your own lookup algorithm. But what really confused me a little was that the following is not working either: public class TestClass { public String doSomething(Serializable s) { return s.toString(); } public static void main(String[] args) throws SecurityException, NoSuchMethodException { TestClass tc = new TestClass(); Method m = tc.getClass().getMethod("doSomething",

Getting generic parameter from supertype class

守給你的承諾、 提交于 2019-12-31 01:48:12
问题 Say I have a parent interface/class like so interface Parent<T> {} And a number of implementing interfaces that fix the generic type. interface Child extends Parent<Type> {} Can I use reflection to get the instance of Class representing T if I have the Class object for Child . Something like this: <T, I extends Parent<T>> I create(Class<I> type) { Class<T> tType = ... ... } Currently I'm having tType be passed in as a parameter, but I'd like to simplify things if I can. 回答1: Yes, despite what

Kotlin language get class at runtime

三世轮回 提交于 2019-12-31 00:36:11
问题 Let's say that we have the following: val person = "Bill" Could someone explain the difference between these two: val kClass1 = person.javaClass.kotlin vs val kClass2 = person::class When I should call the one instead of the other? Any source code example would be appreciated. 回答1: The main reason there are two ways to achieve the same thing, namely get the Kotlin class of an object, is because prior to Kotlin 1.1, the ::class literal did not support the expression on its left-hand side. So

Scala: cross (cartesian) product with multiple sources and heterogeneous types

故事扮演 提交于 2019-12-30 23:50:11
问题 I'm trying to construct multiple cross products of traversables of different (but each homogeneous) types. The desired return type is a traversable of a tuple with the type matching the types in the input traversables. For example: List(1, 2, 3) cross Seq("a", "b") cross Set(0.5, 7.3) This should give a Traversable[(Int, String, Double)] with all possible combinations from the three sources. The case of combining only two sources was nicely answered here. The given idea is: implicit class

How to get generic type definition for CRTP type

瘦欲@ 提交于 2019-12-30 18:54:13
问题 Given the following CRTP type in C#: public abstract class DataProviderBase<TProvider> where TProvider : DataProviderBase<TProvider> { } How would I get its generic type definition in F#? let typeDef = typedefof<DataProviderBase<_>> yields the error: Type constraint mismatch when applying the default type 'DataProviderBase<'a>' for a type inference variable. The resulting type would be infinite when unifying ''a' and 'DataProviderBase<'a>' Consider adding further type constraints In C#, it

Why do I have the wrong number of arguments when calling main through reflection?

纵然是瞬间 提交于 2019-12-30 18:33:19
问题 I have a Class object and I want to invoke a static method. I have the following code. Method m=cls.getMethod("main",String[].class); System.out.println(m.getParameterTypes().length); System.out.println(Arrays.toString(m.getParameterTypes())); System.out.println(m.getName()); m.invoke(null,new String[]{}); This prints: 1 [class [Ljava.lang.String;] main But then it then it throws: IllegalArgumentException: wrong number of arguments What am I overlooking here? 回答1: You will have to use m

How do I get a selector from its name?

删除回忆录丶 提交于 2019-12-30 18:00:08
问题 I have an NSString containing the name of a selector I would like to call with performSelector. How can I get a reference to the selector from the string? 回答1: NSSelectorFromString(name) 回答2: Could use NSSelectorFromString. Probably inefficient as parameterizing a SEL is preferred. 来源: https://stackoverflow.com/questions/1174985/how-do-i-get-a-selector-from-its-name