How to get concrete object of a static method via mirror API?

旧巷老猫 提交于 2019-12-10 03:42:07

问题


I have something like this:

class MyClass
{
   static void DoSomething(arg1, arg2){...}
}

Via reflection, I am able to get the ClassMirror of this class. From this point, how would I get to the concrete static function so I can call it.

Note that I tried to use:

ObjectMirror.invoke('DoSomething', [arg1, arg2]); 

which would initially appear to work, but it doesn't support passing of complex types as arguments, This static function requires a complex type as one of it's arguments.

Ideally, I'd like to get the 'Function' object that represents the static method so I can invoke it directly.


回答1:


a. The current state of affairs is temporary. The plan is that the mirror API will wrap the arguments with mirrors for you.

b. The API may eventually support a getProperty method that will give you a Future on the function object. However, you will not get a Function object directly, so this won't really make any difference in this case.

c. The core idea is that the API fundamentally works on mirrors. To make it more usable, it should accept non-mirrors as input and wrap them in mirrors for you. It will always return mirrors, and in some cases return futures on these. This is so the API works the same for remote and local cases.

d. Resources for understanding mirrors:

  1. http://www.bracha.org/mirrors.pdf (academic paper, tough going)
  2. http://www.hpi.uni-potsdam.de/hirschfeld/events/past/media/100105_Bracha_2010_LinguisticReflectionViaMirrors_HPI.mp4 (a video, pre-Dart, discusses earlier Mirror systems)
  3. http://gbracha.blogspot.com/2010/03/through-looking-glass-darkly.html (an old, pre-dart, blog post of mine on mirrors)
  4. http://www.wirfs-brock.com/allen/posts/228 (Allen Wirfs-Brock's blog. Allen was a mirror pioneer back in Smalltalk in the 90s)
  5. http://www.wirfs-brock.com/allen/posts/245

You can also search my blog, or Allen Wirf-Brock's for posts on the topic.



来源:https://stackoverflow.com/questions/12113647/how-to-get-concrete-object-of-a-static-method-via-mirror-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!