Hey Dave and Adam,
That way a quick reply i sent yesterday without much thought.
Currently to convert a generic object to a typed instance i've been
using a utility provided by Darron Schall.
http://www.darronschall.com/weblog/archives/000247.cfm
basically the method looks like this,
public static function objectToInstance( object:Object, clazz:Class ):*
{
var bytes:ByteArray = new ByteArray();
bytes.objectEncoding = ObjectEncoding.AMF0;
// Find the objects and byetArray.writeObject them, adding in
the
// class configuration variable name -- essentially, we're
constructing
// and AMF packet here that contains the class information so
that
// we can simplly byteArray.readObject the sucker for the
translation
// Write out the bytes of the original object
var objBytes:ByteArray = new ByteArray();
objBytes.objectEncoding = ObjectEncoding.AMF0;
objBytes.writeObject( object );
// Register all of the classes so they can be decoded via AMF
var typeInfo:XML = describeType( clazz );
var fullyQualifiedName:String = [EMAIL PROTECTED]().replace
( /::/, "." );
registerClassAlias( fullyQualifiedName, clazz );
// Write the new object information starting with the class
information
var len:int = fullyQualifiedName.length;
bytes.writeByte( 0x10 ); // 0x10 is AMF0 for "typed object (class
instance)"
bytes.writeUTF( fullyQualifiedName );
// After the class name is set up, write the rest of the object
bytes.writeBytes( objBytes, 1 );
// Read in the object with the class property added and return
that
bytes.position = 0;
// This generates some ReferenceErrors of the object being
passed in
// has properties that aren't in the class instance, and generates
TypeErrors
// when property values cannot be converted to correct values (such
as false
// being the value, when it needs to be a Date instead). However,
these
// errors are not thrown at runtime (and only appear in trace ouput
when
// debugging), so a try/catch block isn't necessary. I'm not sure
if this
// classifies as a bug or not... but I wanted to explain why if you
debug
// you might seem some TypeError or ReferenceError items appear.
var result:* = bytes.readObject();
return result;
}
regards,
Bjorn
On 18/10/2007, at 10:40 AM, dave_defusion wrote:
Bjorn,
I have been looking at this (as I would like a mocking framework for
Flex too) this evening and I've had no luck either.
The the registerClassAlias() does add the alias to a given class I
can't then get anything to accept it as being of that type
- I'm not sure what the alias is, but dumping the describeType()
results shows it as an alias property on the type and not as an
implementsInterface node, which is what we need.
I may not have been fully understood what registerClassAlias though.
But it looks (ok, it's late and I'm tired so it may just be that which
is causing my negativity) as if trying to get a class to implement an
interface (or at least appear to implement an interface) dynamically
at run time is a futile effort.
Any thoughts?
-D
--- In flexcoders@yahoogroups.com, Bjorn Schultheiss
<[EMAIL PROTECTED]> wrote:
>
> try add
> flash.net.registerClassAlias( "attempt.TestInterface",
TestInterface );
> prior to your cast attempt.
>
>
> regards,
>
> Bjorn
>
>
> On 17/10/2007, at 1:06 PM, aduston1976 wrote:
>
> > lus I see a number of places on the web where the idea is
mentioned,
> > e.g. http://www.herrodius.com/blog/?m=200704 . However I cannot
find
> > any mock frameworks for AS3.
> >
> > This evening I spent some time trying to mock one up on my own
(ok,
> > sorry for the pun!) but I ran into troubles. Is anyone
interested in
> > thinking about this problem with me?
> >
> > Here is what I did during my 20 minutes of playing:
> >
> > Define an interface:
> >
> > public interface TestInterface
> > {
> > function setSomething(something : String) : void;
> > }
> >
> > Run the following code:
> >
> > public function flexmock() {
> > var i : TestInterface;
> > var c : Class =
> > flash.utils.getDefinitionByName("attempt.TestInterface") as Class;
> > var desc : XML = flash.utils.describeType(c);
> > var str : String = desc.toString();
> > var a : Object = new Object();
> > a["setSomething"] = function(something : String) : void {
> > trace(something); };
> > // will fail on the following line, since a does not implement
> > TestInterface.
> > i = TestInterface(a);
> > i.setSomething("blah");
> > }
> >
> > AS3 doesn't support duck typing like Ruby, and I believe there
is no
> > way to get an object in memory that implements the TestInterface
> > interface that my other objects can use as a mock.
> >
> > Any thoughts about this? Am I doomed to use elbow grease or code
> > generation to create these things?
> >
> > BTW, this message is also posted on the ActionScript 3 FlexUnit
> > Library forum in Google Groups. Hopefully that isn't some kind of
> > forum faux pas.
> >
> > Thank you for any comments,
> > Adam
> >
> >
> >
>