<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Lesnikowski Blog &#187; PostSharp</title>
	<atom:link href="http://www.lesnikowski.com/blog/tag/postsharp/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.lesnikowski.com/blog</link>
	<description>EMAIL IMAP POP3 SMTP FTP FTPS barcode components blog</description>
	<lastBuildDate>Sun, 05 Feb 2012 14:10:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Cross-thread operations with PostSharp</title>
		<link>http://www.lesnikowski.com/blog/cross-thread-operations-with-postsharp/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=cross-thread-operations-with-postsharp</link>
		<comments>http://www.lesnikowski.com/blog/cross-thread-operations-with-postsharp/#comments</comments>
		<pubDate>Tue, 26 Jan 2010 15:00:43 +0000</pubDate>
		<dc:creator>Pawel Lesnikowski</dc:creator>
				<category><![CDATA[Presentation]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[PostSharp]]></category>
		<category><![CDATA[WindowsForms]]></category>

		<guid isPermaLink="false">http://www.lesnikowski.com/blog/?p=101</guid>
		<description><![CDATA[When you try to inform User Interface (UI) about the background operation progress or completion, you can not do it from the background thread. UI doesn&#8217;t like to be informed about anything from a different thread: you&#8217;ll get &#8220;System.InvalidOperationException: Cross-thread operation not valid: Control &#8216;xxx&#8217; accessed from a thread other than the thread it was [...]]]></description>
			<content:encoded><![CDATA[<p>When you try to <strong> inform User Interface</strong> (UI) about the <strong>background operation progress</strong> or <strong>completion</strong>, you can not do it from the background thread.</p>
<p>UI doesn&#8217;t like to be informed about anything from a different thread: you&#8217;ll get <strong>&#8220;System.InvalidOperationException: Cross-thread operation not valid: Control &#8216;xxx&#8217; accessed from a thread other than the thread it was created on.&#8221;</strong> exception from WinForms control, if you try:</p>
<pre class="brush: csharp;">
public void ShowStatus(ApplicationStatus status)
{
    this._lblServiceAddress.Text = &quot;Connected to: &quot;
        + status.WebServiceAddress;
    this._lblUserId.Text = &quot;Working as: &quot;
        + status.UserId;
}
</pre>
<p>The easiest sollution is to use <em>BeginInvoke </em>method on the control <em>Control</em> or <em>Form</em>:</p>
<pre class="brush: csharp;">
public void ShowStatus(ApplicationStatus status)
{
    this.BeginInvoke((MethodInvoker)(() =&gt;
        {
            this._lblServiceAddress.Text = &quot;Connected to: &quot;
                + status.WebServiceAddress;
            this._lblUserId.Text = &quot;Working as: &quot;
                + status.UserId;
        }));
}
</pre>
<p>Well, it&#8217;s fun to write this once, but if you have many operations done in background sooner or later you&#8217;d like to have something nicer. Like an <strong>attribute</strong> for example:</p>
<pre class="brush: csharp;">
[ThreadAccessibleUI]
public void ShowStatus(ApplicationStatus status)
{
    this._lblServiceAddress.Text = &quot;Connected to: &quot; + status.WebServiceAddress;
    this._lblUserId.Text = &quot;Working as: &quot; + status.UserId;
}
</pre>
<p>Here&#8217;s the attribute implementation of such attribute using PostSharp 1.5:</p>
<pre class="brush: csharp;">
/// &lt;summary&gt;
/// PostSharp attribute.
/// Use it to mark Control's methods that
/// are invoked from background thread.
/// &lt;/summary&gt;
/// &lt;remarks&gt;
/// Be careful as BeginInvoke uses the message queue.
/// This means that the interface will be refreshed
/// when application has a chance to process its messages.
/// &lt;/remarks&gt;
[AttributeUsage(AttributeTargets.Method)]
[Serializable] // required by PostSharp
public class ThreadAccessibleUIAttribute : OnMethodInvocationAspect
{
    public override void OnInvocation(
        MethodInvocationEventArgs eventArgs)
    {
        Control control = eventArgs.Instance as Control;
        if (control == null)
            throw new ApplicationException(
                &quot;ThreadAccessibleUIAttribute&quot; +
                &quot;can be applied only to methods on Control class&quot;);

        // The form may be closed before
        // this method is called from another thread.
        if (control.Created == false)
            return;

        control.BeginInvoke((MethodInvoker)eventArgs.Proceed);
    }
};
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.lesnikowski.com/blog/cross-thread-operations-with-postsharp/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Introducing PostSharp to your team</title>
		<link>http://www.lesnikowski.com/blog/introducing-postsharp-to-your-team/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=introducing-postsharp-to-your-team</link>
		<comments>http://www.lesnikowski.com/blog/introducing-postsharp-to-your-team/#comments</comments>
		<pubDate>Wed, 16 Sep 2009 12:17:30 +0000</pubDate>
		<dc:creator>Pawel Lesnikowski</dc:creator>
				<category><![CDATA[AOP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[PostSharp]]></category>

		<guid isPermaLink="false">http://www.lesnikowski.com/blog/?p=30</guid>
		<description><![CDATA[PostSharp is AOP (Aspect-Oriented-Programming) framework (http://www.postsharp.org/). It transparently inserts itself in the build process and post-processes the compiled assembly. To enable PostSharp in your project you need to download and run the PostSharp installer, and add appropriate references in your project. However if your project is in SVN, and you are not the only one [...]]]></description>
			<content:encoded><![CDATA[<p>PostSharp is <strong>AOP</strong> (Aspect-Oriented-Programming) framework (<a href="http://www.postsharp.org/">http://www.postsharp.org/</a>).<br />
It transparently inserts itself in the build process and post-processes the compiled assembly.</p>
<p>To enable PostSharp in your project you need to download and run the PostSharp installer, and add appropriate references in your project.</p>
<p>However if your project is in SVN, and you are not the only one compiling it (other developers, CI machine) most likely you don&#8217;t want to run PostSharp installer on all the machines.</p>
<p>There is a way to introduce PostSharp <strong>transparently </strong> to your project.</p>
<ol>
<li>
Put all the PostSharp files in the LibPostSharp folder of your project <strong>and add this folder to SVN</strong>.
</li>
<li>
<strong>Modify the .csproj file</strong>:<br />
After the last ItemGroup following xml must be inserted:</p>
<pre class="brush: xml;">
  &lt;PropertyGroup&gt;
    &lt;DontImportPostSharp&gt;True&lt;/DontImportPostSharp&gt;
    &lt;!-- Add the next line if you are using Visual Studio 2010 --&gt;
    &lt;!-- &lt;PostSharpUseCommandLine&gt;True&lt;/PostSharpUseCommandLine&gt;--&gt;
  &lt;/PropertyGroup&gt;
</pre>
</li>
<li>
<strong>Modify the .csproj file</strong>:<br />
After the</p>
<pre class="brush: xml;">
&lt;Import Project=&quot;$(MSBuildToolsPath)Microsoft.CSharp.targets&quot; /&gt;
</pre>
<p>following xml must be inserted:</p>
<pre class="brush: xml;">
&lt;Import Project=&quot;..LibPostSharpPostSharp.targets&quot; /&gt;
</pre>
<p>The whole change should be similar to this sample:</p>
<pre class="brush: xml;">
&lt;/ItemGroup&gt;
  &lt;PropertyGroup&gt;
    &lt;DontImportPostSharp&gt;True&lt;/DontImportPostSharp&gt;
  &lt;/PropertyGroup&gt;
  &lt;Import Project=&quot;$(MSBuildToolsPath)Microsoft.CSharp.targets&quot; /&gt;
  &lt;Import Project=&quot;..LibPostSharpPostSharp-1.5.targets&quot; /&gt;
&lt;/Project&gt;
</pre>
</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.lesnikowski.com/blog/introducing-postsharp-to-your-team/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>INotifyPropertyChanged with PostSharp 1.5</title>
		<link>http://www.lesnikowski.com/blog/inotifypropertychanged-with-postsharp/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=inotifypropertychanged-with-postsharp</link>
		<comments>http://www.lesnikowski.com/blog/inotifypropertychanged-with-postsharp/#comments</comments>
		<pubDate>Tue, 15 Sep 2009 12:03:01 +0000</pubDate>
		<dc:creator>Pawel Lesnikowski</dc:creator>
				<category><![CDATA[AOP]]></category>
		<category><![CDATA[Presentation]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[MVVM]]></category>
		<category><![CDATA[PostSharp]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.lesnikowski.com/blog/?p=14</guid>
		<description><![CDATA[If you are doing WPF development, most likely you are tired of writing property implementations that raise PropertyChanged event manually: public class MainWindowViewModel : ViewModel { private string _message; public string Message { get { return _message; } set { _message = value; OnPropertyChanged(&#34;Message&#34;); } } // ... } PostSharp is a great tool to [...]]]></description>
			<content:encoded><![CDATA[<p>If you are doing WPF development, most likely you are tired of writing property implementations that raise <em>PropertyChanged</em> event manually:</p>
<pre class="brush: csharp;">
public class MainWindowViewModel : ViewModel
{
    private string _message;

    public string Message
    {
        get
        {
            return _message;
        }
        set
        {
            _message = value;
            OnPropertyChanged(&quot;Message&quot;);
        }
    }

    // ...
}
</pre>
<p><a href="http://www.postsharp.org">PostSharp</a> is a great tool to make such things simplier.</p>
<p>Let&#8217;s look at the specific ViewModel class that has a <em>Message</em> property that is <strong>bound to some UI element</strong> using XAML:</p>
<pre class="brush: csharp;">
public class MainWindowViewModel : ViewModel
{
    [RaisePropertyChanged]
    public string Message { get; set; }

    // ...
}
</pre>
<p> Notice the <em>RaisePropertyChanged</em> attribute, which we&#8217;ll implement later.</p>
<p>Here&#8217;s our <strong>base ViewModel</strong> class that provides <strong>actual implementation</strong> of the <em>INotifyPropertyChanged</em> interface:</p>
<pre class="brush: csharp;">
public class ViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged
        = delegate { };

    public void OnPropertyChanged(string propertyName)
    {
        PropertyChanged(this,new PropertyChangedEventArgs(propertyName));
    }
};
</pre>
<p>Finally the PostSharp attribute:</p>
<pre class="brush: csharp;">
[Serializable]  // required by PostSharp
public class RaisePropertyChangedAttribute : OnMethodBoundaryAspect
{
    private string _propertyName;

    /// &lt;summary&gt;
    /// Executed at runtime, after the method.
    /// &lt;/summary&gt;
    public override void OnExit(MethodExecutionEventArgs eventArgs)
    {
        ViewModel viewModel = (ViewModel)eventArgs.Instance;
        viewModel.OnPropertyChanged(_propertyName);
    }

    public override bool CompileTimeValidate(MethodBase method)
    {
        if (IsPropertySetter(method))
        {
            _propertyName = GetPropertyName(method);
            return true;
        }
        return false;
    }

    private static string GetPropertyName(MethodBase method)
    {
        return method.Name.Replace&quot;set_&quot;, &quot;&quot;);
    }

    private static bool IsPropertySetter(MethodBase method)
    {
        return method.Name.StartsWith(&quot;set_&quot;);
    }
};
</pre>
<p>Note that we are validating if the method is in fact a property only during <strong>compilation time</strong> using <em>CompileTimeValidate</em> method.</p>
<p>During compile time appropriate invocations of <em>OnPropertyChanged</em> method will be <strong>injected after every set operation</strong> applied to the <em>Message</em> property.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lesnikowski.com/blog/inotifypropertychanged-with-postsharp/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

