开发者

Alternative in ActionScript for "DataMember" in C#

currently I'm translating an app from C# into ActionScript (Silverlight to Flex) and I need to serialize some XML content in ActionScript.

Now, unfortunately I need the variables to have an other name than the nodes in the XML file. I tried a workaround with getters and setters. It worked for setting the variables, but I failed at the getters, because when you make the variable private the native AS3 'SimpleXMLEncoder' ignores it.

The best solution would be, if I could give aliases to the variables. Thats the way it worked in C#. Just like this:

[DataMember(Name = "some")]
public someVariable Typ开发者_如何转开发e { get; set; }

Is there any way to do something similar in ActionScript? I only found this one, which is for classes:

[RemoteClass(alias="user")]

I hope I described my first question well, and my english is ok. Thank you. :)


it's funny because I just finished writing one of these for work after seeing how handy it was it C#. We may be releasing it as a library soon, but until then I can point you in the direction of the SpiceLib, specifically there section on using custom metadata here.

What we did was define our own metadata (attributes) tags called 'DataMember' and then created a serializer that would translate the Typed classes into Dynamic Objects such as:

[DataMember(Name="some")]
public var someVariable:Type;


//in Type, no MetaTags means we use the given accessor

public var someName:String;

//becomes {some:{someName:""}}

We then run this Dynamic Object through either the Adobe JSON encoder, or use the Flex XML serializer.

Hope this helps, will let you know if and when we release our library.


Not sure if there's a way to do it with the class itself, but could you add a method to your class that outputs a new object for use with XML serialization, and pass that to the SimpleXMLEncoder? In the object you return you'd set things to the names you'd want them to be, regardless of the actual variable names in the class.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜