Silverlight directive confusion

Silverlight directive confusion

Old forum URL: forums.lhotka.net/forums/t/11137.aspx


griff posted on Wednesday, February 08, 2012

Hi

I've suddenly just got a little confused over the SILVERLIGHT directive.  I've just generated some code (cslagenfork) - a string field as below

       /// <summary>
        /// Maintains metadata about <see cref="SchoolName"/> property.
        /// </summary>
#if SILVERLIGHT
        [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
        public static readonly PropertyInfo<string> SchoolNameProperty = RegisterProperty<string>(p => p.SchoolName, "School Name");
#else
        private static readonly PropertyInfo<string> SchoolNameProperty = RegisterProperty<string>(p => p.SchoolName, "School Name");
#endif
        /// <summary>
        /// Gets or sets the School Name.
        /// </summary>
        /// <value>The School Name.</value>
        public string SchoolName
        {
            get { return GetProperty(SchoolNameProperty); }
            set { SetProperty(SchoolNameProperty, value); }
        }

Then from ProjectTracker

    public static readonly PropertyInfo<string> NameProperty = RegisterProperty<string>(c => c.Name);
    [Display(Name = "Project name")]
    [Required]
    [StringLength(50)]
    public string Name
    {
      get { return GetProperty(NameProperty); }
      set { SetProperty(NameProperty, value); }
    }
1. Why does the first example have the SL directive and not the second and vice-versa.
and similary this code.....
    public static void CloseProject(int id, EventHandler<DataPortalResult<ProjectCloser>> callback)
    {
      ProjectCloser cmd = new ProjectCloser { ProjectId = id };
      DataPortal.BeginExecute<ProjectCloser>(cmd, callback);
    }
 
#if !SILVERLIGHT
    public static ProjectCloser CloseProject(int id)
    {
      ProjectCloser cmd = new ProjectCloser { ProjectId = id };
      cmd = DataPortal.Execute<ProjectCloser>(cmd);
      return cmd;
    }
#endif

2. Why doesn't the
public static void CloseProject(int id, EventHandler<DataPortalResult<ProjectCloser>> callback)
    {
      ProjectCloser cmd = new ProjectCloser { ProjectId = id };
      DataPortal.BeginExecute<ProjectCloser>(cmd, callback);
    }
not have directive around.
#if SILVERLIGHT
...........
#endif

I thought I had this understood but am having a senior moment or missing something.
Thanks
Richard

JonnyBee replied on Wednesday, February 08, 2012

Hi,

1. For Silverlight the PropertyInfo must be public to be initalized correctly. It may also be public for other platforms but this is not required.

EditorBrowsable attribute is used to hide this element from appearing in intellisense (Visual Studio)

Many developers (and the samples)  now tend to always make the PropertyInfo public to keep the code as simple as possible and the same for all platforms. Whether you want to use the EditorBrowsable attribute to hide the PropertInfo from appearing in Visual Studios Intellisense is optional up to you.

2. The async method is common for both SL and .NET (needs no compiler directive)
 The sync method is only for .NET (not SL) and hence the  #if !SILVERLIGHT to not include code in SL.

 

 

 

griff replied on Wednesday, February 08, 2012

Hi

thanks for the prompt reply.  If I understand you correctly, if I'm not bothered about hiding the propertyinfo then I can declare as below....

public static readonly PropertyInfo<string> SchoolNameProperty = RegisterProperty<string>(p => p.SchoolName, "School Name");
.......
Richard

JonnyBee replied on Wednesday, February 08, 2012

There's nothing wrong about using this syntax on all platforms if you wish to hide the PropertyInfo's:

[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
public static readonly PropertyInfo SchoolNameProperty = RegisterProperty(p => p.SchoolName, "School Name");

Or leaving them as public static.

Read more here: http://forums.lhotka.net/forums/p/10921/50877.aspx#50877

rcollette replied on Wednesday, February 08, 2012

Wow.. talk about breaking encapsulation.   What is it about Silverlight that requires a static field to be public?  I'm going to guess that Reflection was being used in the initialization of PropertyInfo.   I am a little sad to see that the cross platform nature of CSLA.NET is not quite what I thought it was.  Does it run, yes.  Does it cause me to have to design against normal OO design practices for Silverlight, yes.

JonnyBee replied on Wednesday, February 08, 2012

@rcolettte: It is due to the restricted reflection in Silverlight.

If you make all PropertyInfo public (with or without EditorBrowsable) it will work on all platforms.

CSLA can not alter the restrictions on Reflection in Silverlight.

 

tiago replied on Wednesday, February 08, 2012

As a side note, that are other public visibility requisites for Silverlight code. Read more about it in Technical Note: Silverlight code for CSLA 4

Copyright (c) Marimer LLC