Properties as fields

19 views
Skip to first unread message

rick...@gmail.com

unread,
May 20, 2015, 9:26:12 AM5/20/15
to shar...@googlegroups.com
Hi friends, I'm using Sharpkit Express at my application, but there´s some weird stuff happening.

When it converts this class:

    [DataContract]
    [JsType(JsMode.Json, AutomaticPropertiesAsFields = true, PropertiesAsFields = true)]
    public class DTOProcesso : ITranslate
    {
        [DataMember]
        [JsProperty(NativeField = true)]
        public int? NumeroProcesso { get; set; }

        [DataMember]
        [JsProperty(NativeField = true)]
        public int? NumeroProcessoAnterior { get; set; }

And I call this piece of code at my code:
var x = dtoProcesso.NumeroProcessoAnterior.Value.ToString();

It will generate this code:
var x = dtoProcesso.NumeroProcessoAnterior.get_Value().toString();

Which will end up in an error, since my class doesn´t have the property getter.
What´s the point? I've decorated the class trying to make Sharpkit generate the properties as fields, but without success.

Dan-el Khen

unread,
May 20, 2015, 9:33:10 AM5/20/15
to shar...@googlegroups.com
Hi,

.NET Nullables aren't exactly fully supported in runtime. 

One simple option is to use JsNumber instead of int?, which is nullable since it's a class. But it looks like you're trying to re-use your server-side code, so in your case, you can either avoid the use of the 'Value' property of Nullable, it's pretty simple if you use the .As<T>() extension method, or simply treat the int? as if it's an int, examples:

if(p.NumeroProcesso==null) { }
if(p.NumeroProcesso>7) { }
var x = p.NumeroProcesso.As<JsNumber>();
//use x natively from here


Another thing you can do, is to remap the Nulleable<T>.Value property as a Name="" using some SharpKit attributes, it goes something like this I think:
[assembly: JsProperty(TargetType=typeof(Nullable<T>), TargetProperty="Value", Name="", NativeField=true)]

If you need help with this just let me know.

Cheers
Dan-el Khen


--
You received this message because you are subscribed to the Google Groups "SharpKit Support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sharpkit+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

rick...@gmail.com

unread,
May 20, 2015, 9:42:29 AM5/20/15
to shar...@googlegroups.com
Wow, thanks for the quick answer!
I removed the .value part and it works pretty well.

Last question, not wanting to abuse, but there is a specific reason for a very similar class with this be generating enumerators with the "GetEnumerator()" syntax , even though with the attributes "NativeEnumerator, NativeArrayEnumerator" signed?

Again, lot of thanks.

Dan-el Khen

unread,
May 20, 2015, 10:52:32 AM5/20/15
to shar...@googlegroups.com
I'm not sure if I understand your question, can you provide a code example for it? 

By the way, another small thing, you can remove AutomaticPropertiesAsFields=true as well as PropertiesAsFields, since in Json mode these are set by default. You can see it here:

Cheers
D.
Reply all
Reply to author
Forward
0 new messages