0

I have a class defined in a Portable Class Library. In that class I have an int that I only want to be have the [DataMember] attribute if the code is running on WP8, how to achieve this?

There should be a way to use #IF but I'm not sure of the syntax and the conditionals, something like this(pseudo-syntax):

#if (condition for WP8) then
include this line: [DataMember]
public int TrackingId { get; set; }

Also, how could I check for other environments than WP8?

neo112
  • 1,703
  • 2
  • 17
  • 39
  • [Conditional compilation](http://stackoverflow.com/questions/20575969/removing-list-items-without-any-other-reference) is determined at compilation time , not at the run-time. – L.B Dec 13 '13 at 21:22
  • but is there a way to include the [DataMember] attribute somehow only when the code is running on WP8? – neo112 Dec 13 '13 at 22:07
  • 1
    You'll need to have your implementation in your WP8 project. PCL have no knowledge of environment at compile time – Shawn Kendrot Dec 13 '13 at 22:12
  • 1
    neo112, it seems like an [XY-problem](http://www.perlmonks.org/?node_id=542341). How about explaining what you really try to achieve? Maybe people can suggest other ways that can be done without *Conditional compilation* – L.B Dec 13 '13 at 22:12
  • I'm trying to reuse Model classes from WP8 project in a WebApi project. I need the TrackingId attribute for Serialization on the WP8 client however there is no such need on the WebApi project, in fact, I'd prefer the Json output not contain the TrackingId – neo112 Dec 13 '13 at 22:28

1 Answers1

-2

try wrapping your [DataMember] attribute with

#if WINDOWS_PHONE8

#endif

like

public class SomeContract
{
#if WINDOWS_PHONE8
    [DataContract]
#endif
    public string MyProperty { get; set; }
}
Kralizek
  • 1,999
  • 1
  • 28
  • 47
  • i wrapped just the [DataMember] attributed in the if and then tried adding [DataMember]+property to if and just property to else but that didnt work either – neo112 Dec 13 '13 at 22:01
  • 1
    @neo112 Why? because `#if WINDOWS_PHONE8` is handled by the compiler. So It creates code according to that directive. Not available in runtime. Compiler's output is either for WP8 or not, depending on the compile time conditions. – L.B Dec 13 '13 at 22:07
  • That's exactly what the question is about :O – Kralizek Dec 13 '13 at 22:29
  • @Kralizek I think you don't understand what you read: *I only want to be have the [DataMember] attribute if the code is running on WP8*. **Not at compile time** – L.B Dec 13 '13 at 22:32