<?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>Avivo Tech Blog &#187; class</title>
	<atom:link href="http://tech.avivo.si/tag/class/feed/" rel="self" type="application/rss+xml" />
	<link>http://tech.avivo.si</link>
	<description>Solving problems</description>
	<lastBuildDate>Tue, 24 Jan 2012 14:46:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>Silverlight: inherit a generic class from UserControl</title>
		<link>http://tech.avivo.si/2009/11/silverlight-inherit-a-generic-class-from-usercontrol/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=silverlight-inherit-a-generic-class-from-usercontrol</link>
		<comments>http://tech.avivo.si/2009/11/silverlight-inherit-a-generic-class-from-usercontrol/#comments</comments>
		<pubDate>Mon, 30 Nov 2009 17:57:49 +0000</pubDate>
		<dc:creator>developer</dc:creator>
				<category><![CDATA[Programming Techniques]]></category>
		<category><![CDATA[Silverlight/WPF]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[derive]]></category>
		<category><![CDATA[generic]]></category>
		<category><![CDATA[inherit]]></category>
		<category><![CDATA[mvvm]]></category>
		<category><![CDATA[silverlight]]></category>
		<category><![CDATA[UserControl]]></category>
		<category><![CDATA[view]]></category>
		<category><![CDATA[workaround]]></category>

		<guid isPermaLink="false">http://tech.avivo.si/?p=208</guid>
		<description><![CDATA[Note! This blog post is just an idea than well tested solution, more a review that detailed description. In Silverlight UserControl and Page classes are usually used with xaml. Root tags in xaml define the type, like so: xaml &#60;UserControl x:Class="MyView"&#62; ... &#60;/UserControl&#62; code-behind namespace MyNamespace { class MyView : UserControl } Sometimes, developers want [...]]]></description>
			<content:encoded><![CDATA[<p>Note! This blog post is just an idea than well tested solution, more a review that detailed description.<br />
In Silverlight UserControl and Page classes are usually used with xaml. Root tags in xaml define the type, like so:</p>
<pre class="c-sharp">xaml
&lt;UserControl x:Class="MyView"&gt;
...
&lt;/UserControl&gt;

code-behind
namespace MyNamespace
{
  class MyView : UserControl
}</pre>
<p>Sometimes, developers want to extend UserControl, i.e. as View by MVVM pattern.</p>
<pre class="c-sharp">xaml
&lt;v:ViewBase x:Class="MyNamespace.MyView" xmlns:v="clr-namespace:MyNamespace"&gt;
...
&lt;/v:ViewBase&gt;

code-behind
namespace MyNamespace
{
  class MyView : ViewBase /* ViewBase is a custom class */
}</pre>
<p>Step further, every View should have it&#8217;s specialized model (ViewModel), thus base class could integrate a Model property. To avoid casting one should use generics, like ViewBase&lt;MyViewModel&gt;.</p>
<pre class="c-sharp">xaml
&lt;!-- There's a problem, generics cannot be written in xaml. --&gt;

code-behind
namespace MyNamespace
{
  class MyView : ViewBase&lt;MyViewModel&gt; /* ViewBase is a custom class */
}</pre>
<p>Generics cannot be written directly in xaml? There is a workaround:<br />
1. Use a wrapper, described <a href="http://www.lab101.be/2008/07/silverlight-usercontrol-inheritance/">here</a><br />
2. Create a new non-generic class, like in this sample:</p>
<pre class="c-sharp">xaml
&lt;v:MyViewBase x:Class="MyNamespace.MyView" xmlns:v="clr-namespace:MyNamespace"&gt;
...
&lt;/v:MyViewBase&gt;

code-behind
namespace MyNamespace
{
  class MyView : MyViewBase
}

public abstract class MyViewBase : ViewBase&lt;MyViewModel&gt;
{
  /* non-generic class */
}</pre>
<p><strong>Appendix: ViewBase&lt;TViewModel&gt; class</strong></p>
<pre class="c-sharp">public abstract class ViewBase&lt;TViewModel&gt; : UserControl, INotifyPropertyChanged where TViewModel : ViewModelBase, new()
{
	private TViewModel model;
	public TViewModel Model
	{
		get
		{
			if (this.model == null)
			{
				this.model = new TViewModel();
				base.DataContext = this.model;
			}
			return this.model;
		}
		set
		{
			if (this.model != value)
			{
				this.model = value;
				base.DataContext = this.model;
				OnPropertyChanged("Model");
			}
		}
	}
}</pre>
<p>References:</p>
<ul>
<li><a href="http://stackoverflow.com/questions/400778/usercontrol-that-has-a-generic-class-in-its-inheritance-tree">StackOverflow: UserControl that has a generic class in its inheritance tree</a></li>
<li><a href="http://stackoverflow.com/questions/225878/how-to-correctly-inherit-from-a-usercontrol-defined-in-xaml-in-silverlight">StackOverflow: How to correctly inherit from a UserControl defined in xaml in Silverlight</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://tech.avivo.si/2009/11/silverlight-inherit-a-generic-class-from-usercontrol/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C# sizeof &#8211; Gets a size of a custom class programatically</title>
		<link>http://tech.avivo.si/2009/10/c-sizeof-gets-a-size-of-a-custom-class-programatically/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=c-sizeof-gets-a-size-of-a-custom-class-programatically</link>
		<comments>http://tech.avivo.si/2009/10/c-sizeof-gets-a-size-of-a-custom-class-programatically/#comments</comments>
		<pubDate>Mon, 05 Oct 2009 13:06:08 +0000</pubDate>
		<dc:creator>Avivo</dc:creator>
				<category><![CDATA[Programming Techniques]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[calculate]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[csharp]]></category>
		<category><![CDATA[size]]></category>
		<category><![CDATA[sizeof]]></category>

		<guid isPermaLink="false">http://blog.sweetucan.com/?p=171</guid>
		<description><![CDATA[In .NET, sizeof() works only for basic variable types, like int, double, etc. If you try to use sizeof() for a managed class then compiler reports error: Cannot take the address of, get the size of, or declare a pointer to a managed type Alternative to sizeof() would be System.Runtime.InteropServices.Marshal.SizeOf() but this one works with [...]]]></description>
			<content:encoded><![CDATA[<p>In .NET, <strong>sizeof</strong>() works only for basic variable types, like <em>int, double</em>, etc. If you try to use <em>sizeof()</em> for a <em>managed class</em> then compiler reports error:</p>
<p><em>Cannot take the address of, get the size of, or declare a pointer to a managed type</em></p>
<p>Alternative to <em>sizeof()</em> would be <em>System.Runtime.InteropServices.Marshal.SizeOf()</em> but this one works with unmanaged types (not a native .NET class).</p>
<p>So far, a decent solution would be to serialize object into binary stream and get the stream size. Source code:</p>
<blockquote><p>public static long SizeOf(object obj)<br />
{<br />
long size = 0;</p>
<p>try<br />
{<br />
System.IO.MemoryStream stream = new System.IO.MemoryStream();<br />
BinaryFormatter objFormatter = new BinaryFormatter();<br />
objFormatter.Serialize(stream, obj);<br />
size = stream.Length;<br />
}<br />
catch (Exception ex)<br />
{<br />
}</p>
<p>return size;<br />
}</p></blockquote>
<p>A non-programatic alternative is to use .NET profiler to retrieve object size.</p>
<p>Reference: http://social.msdn.microsoft.com/forums/en-US/clr/thread/b871dee4-6eb5-4dca-be79-b9589a79f5e9/</p>
]]></content:encoded>
			<wfw:commentRss>http://tech.avivo.si/2009/10/c-sizeof-gets-a-size-of-a-custom-class-programatically/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

