getDefinitionByName() 和类可见性

发布于 2025-01-08 20:02:13 字数 228 浏览 0 评论 0原文

我有包含 ModuleManager 的主应用程序。该应用程序加载不同的模块。主应用程序和加载的模块都使用我的自定义 RSL。我需要在 RSL 中获取 Class 对象,该对象是在模块之一中定义的。我正在尝试使用 getDefinitionByName 函数,但由于我的类未在 RSL 中定义,因此出现异常(尽管已加载具有所需类的模块)。是否可以使模块类对 RSL 代码可见并在运行时获取其实例而不更改项目结构?谢谢

I have main application which contains ModuleManager. Different modules are loaded by this application. Both main application and loaded modules use my custom RSL. I need to get Class object in my RSL, which is defined in one of the modules. I'm trying to use getDefinitionByName function, but since my class is not defined in RSL, I get an exception (though module with needed class is loaded). Is it possible to make module classes visible to RSL code and to get the instance of it at runtime without changing project structure? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

度的依靠╰つ 2025-01-15 20:02:13

如果您在类类型的模块(或其接口)上公开属性,则可以注入类的定义,类似于将类定义注入到按钮中以制作图标的方式。

所以,你的模块可能有这样的代码:

protected var _classToMake:Class;

public function get classToMake():Class {
   return _classToMAke;
}

public function set classToMake(value:Class):void {
   if (value != _classTomake) {
      if (value != null) {
         //test to make sure we're making the right thing
         var testClass:SomeType = new value() as SomeType;
         if (testClass != null) {
            _classToMake = value
         } else {
            trace('classToMake must be a definition that makes a class of SomeType');
         }
      }
   }
}

You can inject the definition of the Class if you expose a property on the Module (or its Interface) of type Class, similar to how Class definitions are injected into buttons to make the icons.

So, your Module might have code like this:

protected var _classToMake:Class;

public function get classToMake():Class {
   return _classToMAke;
}

public function set classToMake(value:Class):void {
   if (value != _classTomake) {
      if (value != null) {
         //test to make sure we're making the right thing
         var testClass:SomeType = new value() as SomeType;
         if (testClass != null) {
            _classToMake = value
         } else {
            trace('classToMake must be a definition that makes a class of SomeType');
         }
      }
   }
}
嗼ふ静 2025-01-15 20:02:13

当加载新模块时,指定应用程序域。正如文档所述:

"The ApplicationDomain class is a container for discrete groups of class definitions."

加载 SWF 时,您可以指定一个应用程序域作为加载程序上下文的一部分。

http://help.adobe.com/en_US /FlashPlatform/reference/actionscript/3/flash/system/ApplicationDomain.html

一旦您获得了模块加载到的应用程序域的引用,您就可以调用 getDefinition()应用程序域的方法来获取定义,其方式与 getDefinitionByName() 大致相同

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/system/ApplicationDomain.html#getDefinition()

另请参阅以下文档“使用应用程序域”,详细描述了它们的工作原理。

http://help.adobe.com/en_US/as3/dev/WSd75bf4610ec9e22f43855da312214da1d8f-8000.html

这是示例的复制粘贴,以防万一丢失:

package 
{ 
    import flash.display.Loader; 
    import flash.display.Sprite; 
    import flash.events.*; 
    import flash.net.URLRequest; 
    import flash.system.ApplicationDomain; 
    import flash.system.LoaderContext; 

    public class ApplicationDomainExample extends Sprite 
    { 
        private var ldr:Loader; 
        public function ApplicationDomainExample() 
        { 
            ldr = new Loader(); 
            var req:URLRequest = new URLRequest("Greeter.swf"); 
            var ldrContext:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain); 
            ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler); 
            ldr.load(req, ldrContext);     
        } 
        private function completeHandler(event:Event):void 
        { 
            var myGreeter:Class = ApplicationDomain.currentDomain.getDefinition("Greeter") as Class; 
            var myGreeter:Greeter = Greeter(event.target.content); 
            var message:String = myGreeter.welcome("Tommy"); 
            trace(message); // Hello, Tommy 
        } 
    } 
}

When you load a new module, specify an Application Domain. As the document says:

"The ApplicationDomain class is a container for discrete groups of class definitions."

You specify an Application Domain as part of the Loader Context when you load the SWF.

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/system/ApplicationDomain.html

Once you have a reference to the Application Domain that the module is loaded in to, you can call the getDefinition() method of the Application Domain to get the definition, in much the same way as getDefinitionByName()

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/system/ApplicationDomain.html#getDefinition()

Also see the following document, "Working with Application Domains," for a great description of exactly how they work.

http://help.adobe.com/en_US/as3/dev/WSd75bf4610ec9e22f43855da312214da1d8f-8000.html

And here's a copy paste of the example, just incase is wanders off:

package 
{ 
    import flash.display.Loader; 
    import flash.display.Sprite; 
    import flash.events.*; 
    import flash.net.URLRequest; 
    import flash.system.ApplicationDomain; 
    import flash.system.LoaderContext; 

    public class ApplicationDomainExample extends Sprite 
    { 
        private var ldr:Loader; 
        public function ApplicationDomainExample() 
        { 
            ldr = new Loader(); 
            var req:URLRequest = new URLRequest("Greeter.swf"); 
            var ldrContext:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain); 
            ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler); 
            ldr.load(req, ldrContext);     
        } 
        private function completeHandler(event:Event):void 
        { 
            var myGreeter:Class = ApplicationDomain.currentDomain.getDefinition("Greeter") as Class; 
            var myGreeter:Greeter = Greeter(event.target.content); 
            var message:String = myGreeter.welcome("Tommy"); 
            trace(message); // Hello, Tommy 
        } 
    } 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文