Solving development problems  |  About this blog

Archive for the ‘class’ tag

Silverlight: inherit a generic class from UserControl

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
<UserControl x:Class="MyView">
...
</UserControl>

code-behind
namespace MyNamespace
{
  class MyView : UserControl
}

Sometimes, developers want to extend UserControl, i.e. as View by MVVM pattern.

xaml
<v:ViewBase x:Class="MyNamespace.MyView" xmlns:v="clr-namespace:MyNamespace">
...
</v:ViewBase>

code-behind
namespace MyNamespace
{
  class MyView : ViewBase /* ViewBase is a custom class */
}

Step further, every View should have it’s specialized model (ViewModel), thus base class could integrate a Model property. To avoid casting one should use generics, like ViewBase<MyViewModel>.

xaml
<!-- There's a problem, generics cannot be written in xaml. -->

code-behind
namespace MyNamespace
{
  class MyView : ViewBase<MyViewModel> /* ViewBase is a custom class */
}

Generics cannot be written directly in xaml? There is a workaround:
1. Use a wrapper, described here
2. Create a new non-generic class, like in this sample:

xaml
<v:MyViewBase x:Class="MyNamespace.MyView" xmlns:v="clr-namespace:MyNamespace">
...
</v:MyViewBase>

code-behind
namespace MyNamespace
{
  class MyView : MyViewBase
}

public abstract class MyViewBase : ViewBase<MyViewModel>
{
  /* non-generic class */
}

Appendix: ViewBase<TViewModel> class

public abstract class ViewBase<TViewModel> : 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");
			}
		}
	}
}

References:

Written by developer

November 30th, 2009 at 6:57 pm

C# sizeof – Gets a size of a custom class programatically

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 unmanaged types (not a native .NET class).

So far, a decent solution would be to serialize object into binary stream and get the stream size. Source code:

public static long SizeOf(object obj)
{
long size = 0;

try
{
System.IO.MemoryStream stream = new System.IO.MemoryStream();
BinaryFormatter objFormatter = new BinaryFormatter();
objFormatter.Serialize(stream, obj);
size = stream.Length;
}
catch (Exception ex)
{
}

return size;
}

A non-programatic alternative is to use .NET profiler to retrieve object size.

Reference: http://social.msdn.microsoft.com/forums/en-US/clr/thread/b871dee4-6eb5-4dca-be79-b9589a79f5e9/

Written by Avivo

October 5th, 2009 at 2:06 pm