Showing posts with label Attributes. Show all posts
Showing posts with label Attributes. Show all posts

Saturday, February 7, 2009

XML Serialization Tip: Hiding default constructor

Here is a quick tip. You all know that to serialize and deserialize an object in XML you need a default (parameter less) constructor. But sometimes you don’t want anybody to use it other than the serializer itself.

By making the default constructor obsolete you can make sure no code will directly call it.

public class MyClass
{
	[Obsolete("For XML Serialization Only", true)]
	public MyClass()
	{
		// Needed for XML serailisation
	}

	public MyClass(string initialValue)
	{
		// ...
	}

	// ...
}

Don’t forget to specify true as the second argument to the Obsolete attribute. This will raise a compilation error if this constructor is called directly. I insist on “directly” because nothing will prevent someone to use reflection to call it. XML Serialisation can occur because it uses reflection to do it.

Thursday, November 1, 2007

Tired of stepping through properties while you debug?

If you are tired of stepping through every properties in your code while you are debugging it here are some cool things you can do



Add a DebuggerHidden attribute.



This attribute applies to Properties, Methods and Constructors. So you can do this:






Add a DebuggerStepThrough attribute.



This attribute applies to Methods, Constructors, Structs and Classes. You can use it on a single property but if you have a simple DO (Data Object) you can mark it so the debugger will step through it.


Of course you can also use the entire assembly in release mode.

Source Download: http://www.docdotnet.com/portal/LinkClick.aspx?link=TestSkipDebug.zip&tabid=129&mid=459


Have fun.