reflection

How to access objects within an object by mixing in a trait with reflection?

混江龙づ霸主 提交于 2019-12-29 01:40:30
问题 How can I get all of the objects within an object with reflection? Consider this code: object MonthDay extends MyEnum { //Some important holidays object NewYear extends MonthDay( 1, 1) object UnityDay extends MonthDay(11, 9) object SaintNicholas extends MonthDay(12, 6) object Christmas extends MonthDay(12, 24) } class MonthDay(month: Int, day: Int) trait MyEnum { val values: List[MonthDay] = this.getClass.getField("MODULE$")... val next: MonthDay = ... val previous: MonthDay = ... } //Of

.NET, C#, Reflection: list the fields of a field that, itself, has fields

不问归期 提交于 2019-12-29 01:24:10
问题 In .NET & C#, suppose ClassB has a field that is of type ClassA . One can easily use method GetFields to list ClassB 's fields. However, I want to also list the fields of those ClassB fields that themselves have fields. For example, ClassB 's field x has fields b , s , and i . I'd like to (programmatically) list those fields (as suggested by my comments in the code below). class ClassA { public byte b ; public short s ; public int i ; } class ClassB { public long l ; public ClassA x ; } class

java: unchecked call to getConstructor(java.lang.Class<?>…)

南笙酒味 提交于 2019-12-28 19:36:46
问题 I am using reflection to construct a class (ConfigBuilder) that takes a File as argument: Class myClassType = Class.forName(className); Class[] types = new Class[] { File.class }; Constructor cons = myClassType.getConstructor(types); Object[] constructorArgs = new Object[] { myFile }; cb = (ConfigBuilder) cons.newInstance(constructorArgs); but I get this warning: warning: [unchecked] unchecked call to getConstructor(java.lang.Class<?>...) as a member of the raw type java.lang.Class

get methodinfo from a method reference C#

为君一笑 提交于 2019-12-28 16:36:37
问题 We can use a C# typeof keyword when we want to get Type instance for specified type. But what can I use if I want to get MethodInfo of a method by it's reference? For example I have a simple console app. It contains Program.Main method. I want to get MethodInfo by using something like methodinfoof(Program.Main) . I have this problem because the method names might change, so I cannot just use Type.GetMethodInfo(string MethodName) for that. I have about 10 000 methods for which I would like to

Get value of a parameter of an annotation in Java

无人久伴 提交于 2019-12-28 16:32:10
问题 So I've got a code: @Path("/foo") public class Hello { @GET @Produces("text/html") public String getHtml(@Context Request request, @Context HttpServletRequest requestss){ ... } I am using AspectJ to catch all calls to getHtml method. I would like to get parameters passed to @Produces and to @Path in my advice, i.e. "/foo" and "text/html" in this case. How can I do it using reflection ? 回答1: To get value of the @Path parameter: String path = Hello.class.getAnnotation(Path.class).value();

Invoke method using Reflection on COM Object

筅森魡賤 提交于 2019-12-28 15:58:45
问题 I have an instance of a COM object... which is created like this: Type type = TypeDelegator.GetTypeFromProgID("Broker.Application"); Object application = Activator.CreateInstance(type); When I try to invoke a method: type.GetMethod("RefreshAll").Invoke(application, null); -> type.GetMethod("RefreshAll") returns null . When I try to get all the methods with type.GetMethods() , there is only these methods: GetLifetimeService InitializeLifetimeService CreateObjRef ToString Equals GetHashCode

IllegalAccessException on using reflection

天涯浪子 提交于 2019-12-28 13:46:35
问题 I was trying to learn reflection and I came across this IllegalAccessException. Please see the following code: public class ReflectionTest { public static void main(String[] args) { Set<String> myStr = new HashSet<String>(); myStr.add("obj1"); Iterator itr = myStr.iterator(); Method mtd = itr.getClass().getMethod("hasNext"); System.out.println(m.invoke(it)); } } When I tried to run this program, I got the following: Exception in thread "main" IllegalAccessException I don't understand what's

Julia: invoke a function by a given string

て烟熏妆下的殇ゞ 提交于 2019-12-28 12:05:20
问题 Does Julia support the reflection just like java? What I need is something like this: str = ARGS[1] # str is a string # invoke the function str() 回答1: The Good Way The recommended way to do this is to convert the function name to a symbol and then look up that symbol in the appropriate namespace: julia> fn = "time" "time" julia> Symbol(fn) :time julia> getfield(Main, Symbol(fn)) time (generic function with 2 methods) julia> getfield(Main, Symbol(fn))() 1.448981716732318e9 You can change Main

Julia: invoke a function by a given string

烂漫一生 提交于 2019-12-28 12:05:09
问题 Does Julia support the reflection just like java? What I need is something like this: str = ARGS[1] # str is a string # invoke the function str() 回答1: The Good Way The recommended way to do this is to convert the function name to a symbol and then look up that symbol in the appropriate namespace: julia> fn = "time" "time" julia> Symbol(fn) :time julia> getfield(Main, Symbol(fn)) time (generic function with 2 methods) julia> getfield(Main, Symbol(fn))() 1.448981716732318e9 You can change Main

.NET Assembly Plugin Security

你离开我真会死。 提交于 2019-12-28 11:49:15
问题 I have used the following code in a number of applications to load .DLL assemblies that expose plugins. However, I previously was always concerned with functionality, rather than security. I am now planning to use this method on a web application that could be used by groups other than me, and I would like to make sure that the security of the function is up-to-snuff. private void LoadPlugins(string pluginsDirectory) { List<IPluginFactory> factories = new List<IPluginFactory>(); foreach