Reputation: 4994
Given this:
public namespace foo;
foo var bar:String = "baz";
We can access the property "bar" like so:
var ns:Namespace = foo;
trace(ns::["bar"]);
This doesnt work however:
var ns:Namespace = new Namespace("foo");
trace(ns::["bar"]);
It gives me the following error:
ReferenceError: Error #1069: Property foo::bar not found on MyClass and there is no default value.
On further inspection of the foo namespace I can see (in the debugger) that the URI is in the format {package}:{type}/{name}. If I try to replicate this by doing the following:
var ns:Namespace = new Namespace(getQualifiedClassName(this).replace("::", ":") + "/foo");
trace(ns::["bar"]);
(drumroll)
Still no dice.
Additionally I've tried the following combinations without luck:
var ns:Namespace = new Namespace(null, getQualifiedClassName(this).replace("::", ":") + "/foo");
var ns:Namespace = new Namespace("", getQualifiedClassName(this).replace("::", ":") + "/foo");
Does anyone know if it is possible to get access to namespaced members like this? I don't have compile time access to the namespace, but I can look up it's uri (and prefix) dynamically at run time.
Upvotes: 2
Views: 265
Reputation: 15717
Edit 2: Ok the problem here is that when you create a namespace the compiler create it with the flag package internal, or you have no way in as3 to create a namespace with different access flag (public, protected,...)
So if your namespace is declared public you can get it using the function getDefinition.
Here an example of function that parse your uri and give you back your namespace whereever it is declared in a file or in a class
package XXX {
public namespace foo;
}
or
package YYY {
class XXX {
public namespace foo2;
}
}
Here an example of a function that give you the namespace from the uri
public function getNamespaceFromURI(uri:String, domain:ApplicationDomain = null):Namespace {
if(domain == null) domain = ApplicationDomain.currentDomain;
var tmp:Array = uri.split("/");
var ns:Namespace = null;
var pkg:String = tmp[0].replace(":", ".");
if(tmp.length == 1) {
// namespace declared in a file
ns = domain.getDefinition(pkg) as Namespace;
} else {
// namespace inside a class
var cls:Class = domain.getDefinition(pkg) as Class;
if(cls) {
ns = cls[tmp[1]] as Namespace;
}
}
return ns;
}
Usage :
var ns:namespace = getNamespaceFromURI("YYY:foo");
// or
var ns:namespace = getNamespaceFromURI("YYY:XXX/foo2");
Edit: Ok i missread you have a proplem with a dynamic Namespace name not dynamic property name : So you can access to your created namespace by a static call to your namespace name from the class where it is defined:
class X {
public namespace foo;
}
var ns:Namespace=X['foo'];
So for your example it's:
public namespace foo;
foo var bar:String = "baz";
var ns:Namespace=MyClass['foo'];
trace(ns::['bar'])
i have updated the wonderfl example : http://wonderfl.net/c/7M9O
You can create a new QName with your custom namespace and your dynamic name, then access the property with this[myQName] :
var ns:Namespace=foo;
var qn:QName=new QName(foo, 'bar');
trace(this[qn]);
Here the live example on wonderfl : http://wonderfl.net/c/7M9O
Upvotes: 1