<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-2867299807863618924</id><updated>2012-01-09T21:14:39.681+02:00</updated><category term='General'/><category term='ASP.NET'/><category term='.NET'/><title type='text'>Demo Effect</title><subtitle type='html'>Programming tips and tricks - and occasionally something totally unrelated.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://demoeffect.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2867299807863618924/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://demoeffect.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>teemu</name><uri>http://www.blogger.com/profile/15705736457443484192</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>5</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-2867299807863618924.post-1183766060963828051</id><published>2008-04-21T17:16:00.001+02:00</published><updated>2008-04-21T17:21:05.944+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><title type='text'>Instantiating a Class Without Calling Its Constructor</title><content type='html'>&lt;p&gt;Recently I came across a situation where I wanted to create an instance of a specific class without the constructor logic being executed. My first intuition was to look if reflection had any means to circumvent the constructor logic. Reflection offers a way to instantiate a class by calling its constructor regardless of the constructor's access modifier.&lt;/p&gt;&lt;pre&gt;&lt;span style="color: #008000"&gt;// Call the parameterless constructor of MyClass via reflection.&lt;/span&gt;
&lt;font color="#008080"&gt;Type&lt;/font&gt; t = &lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(&lt;font color="#008080"&gt;MyClass&lt;/font&gt;);
&lt;font color="#008080"&gt;ConstructorInfo&lt;/font&gt; ci = t.GetConstructor(System.&lt;font color="#008080"&gt;Type&lt;/font&gt;.EmptyTypes);
&lt;font color="#008080"&gt;MyClass&lt;/font&gt; o = (&lt;font color="#008080"&gt;MyClass&lt;/font&gt;)ci.Invoke(&lt;span style="color: #0000ff"&gt;null&lt;/span&gt;);&lt;/pre&gt;
&lt;p&gt;Unfortunately reflection didn't help much in bypassing the constructor logic. Next I thought about the &lt;font face="Courier New"&gt;Activator&lt;/font&gt; class and its &lt;font face="Courier New"&gt;CreateInstance&lt;/font&gt; method which supposedly creates an instance of a class by calling the best matching constructor.&lt;/p&gt;&lt;pre&gt;&lt;span style="color: #008000"&gt;// Call the parameterless constructor of MyClass using Activator.&lt;/span&gt;
&lt;font color="#008080"&gt;MyClass&lt;/font&gt; o = (&lt;font color="#008080"&gt;MyClass&lt;/font&gt;)&lt;font color="#008080"&gt;Activator&lt;/font&gt;.CreateInstance(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(&lt;font color="#008080"&gt;MyClass&lt;/font&gt;));&lt;/pre&gt;
&lt;p&gt;If the class doesn't have a constructor &lt;font face="Courier New"&gt;CreateInstance&lt;/font&gt; throws an exception. In other words I wasn't getting any closer to a solution. I was starting to run out of ideas until I recalled that the &lt;font face="Courier New"&gt;SoapFormatter&lt;/font&gt; can deserialize a file into an object without calling the class's constructor. Only when the class implements &lt;font face="Courier New"&gt;ISerializable&lt;/font&gt; interface will the special constructor that takes &lt;font face="Courier New"&gt;SerializationInfo&lt;/font&gt; and &lt;font face="Courier New"&gt;StreamingContext&lt;/font&gt; as parameters be called. So, how exactly do &lt;font face="Courier New"&gt;SoapFormatter&lt;/font&gt; as well as &lt;font face="Courier New"&gt;BinaryFormatter&lt;/font&gt; create instances of classes without calling their constructors? The instances are created by using the static &lt;font face="Courier New"&gt;GetUninitializedObject&lt;/font&gt; from the &lt;font face="Courier New"&gt;FormatterServices&lt;/font&gt; class that contains a number of static helper methods for formatters.&lt;/p&gt;&lt;pre&gt;&lt;span style="color: #008000"&gt;// Create an instance of a class without calling its constructor.&lt;/span&gt;
&lt;font color="#008080"&gt;MyClass&lt;/font&gt; o = (&lt;font color="#008080"&gt;MyClass&lt;/font&gt;)&lt;font color="#008080"&gt;FormatterServices&lt;/font&gt;.GetUninitializedObject(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(&lt;font color="#008080"&gt;MyClass&lt;/font&gt;));&lt;/pre&gt;
&lt;p&gt;That's how easy it is. Of course this method should be used only when other methods for instantiating a class don't suit your needs as there probably is a minor performance hit associated with it.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2867299807863618924-1183766060963828051?l=demoeffect.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://demoeffect.blogspot.com/feeds/1183766060963828051/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2867299807863618924&amp;postID=1183766060963828051' title='9 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2867299807863618924/posts/default/1183766060963828051'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2867299807863618924/posts/default/1183766060963828051'/><link rel='alternate' type='text/html' href='http://demoeffect.blogspot.com/2008/04/instantiating-class-without-calling-its.html' title='Instantiating a Class Without Calling Its Constructor'/><author><name>teemu</name><uri>http://www.blogger.com/profile/15705736457443484192</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>9</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2867299807863618924.post-2393266363065555678</id><published>2008-01-12T13:27:00.001+02:00</published><updated>2008-01-12T13:29:47.962+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><title type='text'>Static Members in Generic Classes</title><content type='html'>&lt;p&gt;The other day I and &lt;a href="http://weblogs.asp.net/alessandro/"&gt;Alessandro&lt;/a&gt;&amp;nbsp;talked about how static members behave in generic classes. This might be blatantly obvious for most people but might cause some confusion for others. Imagine having a generic class of the following nature:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; &lt;font color="#008080"&gt;MyClass&lt;/font&gt;&amp;lt;T&amp;gt;
{
    &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; text = &lt;span style="color: #8b0000"&gt;"uninitialized"&lt;/span&gt;;

    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; Text
    {
        &lt;span style="color: #0000ff"&gt;get&lt;/span&gt; { &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; text; }
        &lt;span style="color: #0000ff"&gt;set&lt;/span&gt; { text = &lt;span style="color: #0000ff"&gt;value&lt;/span&gt;; }
    }
}&lt;/pre&gt;
&lt;p&gt;Now you're calling for example &lt;font face="Courier New" size="2"&gt;&lt;font color="#008080"&gt;MyClass&lt;/font&gt;&amp;lt;&lt;font color="#0000ff"&gt;string&lt;/font&gt;&amp;gt;.Text = &lt;font color="#800000"&gt;"string";&lt;/font&gt;&lt;/font&gt; — What do you think &lt;font face="Courier New" size="2"&gt;&lt;font color="#008080"&gt;MyClass&lt;/font&gt;&amp;lt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&amp;gt;.Text&lt;/font&gt; contains after this? Yup, it still contains the "uninitialized" string. This is due to the fact that at runtime native code is produced specifically for every type. In a sense you could think that there are two separate copies of the generic class, &lt;font face="Courier New" size="2"&gt;MyClass&lt;/font&gt; of &lt;font face="Courier New" size="2"&gt;string&lt;/font&gt; and &lt;font face="Courier New" size="2"&gt;MyClass&lt;/font&gt; of &lt;font face="Courier New" size="2"&gt;int&lt;/font&gt; which both have their own separate static members.&lt;/p&gt;
&lt;p&gt;What if you actually wanted generic classes regardless of the type to share their static members? This is easily achieved by writing a separate static class and making the static members in the generic class act as wrappers for the members of the static class:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; &lt;font color="#008080"&gt;MyClass&lt;/font&gt;&amp;lt;T&amp;gt;
{
    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; Text
    {
        &lt;span style="color: #0000ff"&gt;get&lt;/span&gt; { &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;font color="#008080"&gt;MyClassStatic&lt;/font&gt;.Text; }
        &lt;span style="color: #0000ff"&gt;set&lt;/span&gt; { &lt;font color="#008080"&gt;MyClassStatic&lt;/font&gt;.Text = &lt;span style="color: #0000ff"&gt;value&lt;/span&gt;; }
    }
}

&lt;span style="color: #0000ff"&gt;internal&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; &lt;font color="#008080"&gt;MyClassStatic&lt;/font&gt;
{
    &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; text = &lt;span style="color: #8b0000"&gt;"uninitialized"&lt;/span&gt;;

    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; Text
    {
        &lt;span style="color: #0000ff"&gt;get&lt;/span&gt; { &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; text; }
        &lt;span style="color: #0000ff"&gt;set&lt;/span&gt; { text = &lt;span style="color: #0000ff"&gt;value&lt;/span&gt;; }
    }
}&lt;/pre&gt;
&lt;p&gt;Now both &lt;font face="Courier New" size="2"&gt;&lt;font color="#008080"&gt;MyClass&lt;/font&gt;&amp;lt;&lt;font color="#0000ff"&gt;string&lt;/font&gt;&amp;gt;.Text and &lt;font face="Courier New" size="2"&gt;&lt;font color="#008080"&gt;MyClass&lt;/font&gt;&amp;lt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&amp;gt;.Text&lt;/font&gt;&lt;/font&gt; would refer to the same string.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2867299807863618924-2393266363065555678?l=demoeffect.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://demoeffect.blogspot.com/feeds/2393266363065555678/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2867299807863618924&amp;postID=2393266363065555678' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2867299807863618924/posts/default/2393266363065555678'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2867299807863618924/posts/default/2393266363065555678'/><link rel='alternate' type='text/html' href='http://demoeffect.blogspot.com/2008/01/static-members-in-generic-classes.html' title='Static Members in Generic Classes'/><author><name>teemu</name><uri>http://www.blogger.com/profile/15705736457443484192</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2867299807863618924.post-2501986976434518713</id><published>2007-12-14T00:47:00.001+02:00</published><updated>2007-12-14T08:44:52.902+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><title type='text'>Natural Sort in C#</title><content type='html'>&lt;p&gt;&lt;a href="http://www.wilcob.com/"&gt;Wilco Bauwer&lt;/a&gt; brought &lt;a href="http://www.codinghorror.com/blog/archives/001018.html"&gt;this&lt;/a&gt; article to my attention and as I'm always up for a good challenge I decided to give it a try. As I like regular expressions the first thing I decided was that I would use regular expressions to split the&amp;nbsp;strings within the array&amp;nbsp;instead of string methods and tokens. Now that that was out of the way I still needed to find a way to compare e.g. "10" and "5" so that "5" takes precedence. Obvious solution was to pad the strings with "0" so they would equal in length - i.e. the strings in the previous example would come "10" and "05" respectively. Enough of this chit chat as it's time to let the code speak for itself:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; CompareNaturally(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; a, &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; b)
{
    &lt;font color="#008080"&gt;MatchCollection&lt;/font&gt; m1 = &lt;font color="#008080"&gt;Regex&lt;/font&gt;.Matches(a ?? &lt;span style="color: #8b0000"&gt;""&lt;/span&gt;, &lt;span style="color: #8b0000"&gt;@"\d+|[^\d]+"&lt;/span&gt;);
    &lt;font color="#008080"&gt;MatchCollection&lt;/font&gt; m2 = &lt;font color="#008080"&gt;Regex&lt;/font&gt;.Matches(b ?? &lt;span style="color: #8b0000"&gt;""&lt;/span&gt;, &lt;span style="color: #8b0000"&gt;@"\d+|[^\d]+"&lt;/span&gt;);

    &lt;span style="color: #0000ff"&gt;for&lt;/span&gt; (&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; i = 0, c = 0; i &amp;lt; &lt;font color="#008080"&gt;Math&lt;/font&gt;.Max(m1.Count, m2.Count); i++)
    {
        &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; s1 = i &amp;lt; m1.Count ? m1[i].Value : &lt;span style="color: #8b0000"&gt;""&lt;/span&gt;;
        &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; s2 = i &amp;lt; m2.Count ? m2[i].Value : &lt;span style="color: #8b0000"&gt;""&lt;/span&gt;;
        &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (s1.Length &amp;gt; 0 &amp;amp;&amp;amp; s2.Length &amp;gt; 0 &amp;amp;&amp;amp; &lt;font color="#008080"&gt;Char&lt;/font&gt;.IsDigit(s1[0]) &amp;amp;&amp;amp; &lt;font color="#008080"&gt;Char&lt;/font&gt;.IsDigit(s2[0]))
        {
            &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; len = &lt;font color="#008080"&gt;Math&lt;/font&gt;.Max(s1.Length, s2.Length);
            c = s1.PadLeft(len, &lt;span style="color: #8b0000"&gt;'0'&lt;/span&gt;).CompareTo(s2.PadLeft(len, &lt;span style="color: #8b0000"&gt;'0'&lt;/span&gt;));
        } &lt;span style="color: #0000ff"&gt;else&lt;/span&gt;
            c = s1.CompareTo(s2);

        &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (c != 0)
            &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; c;
    }

    &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; 0;
}
&lt;/pre&gt;
&lt;p&gt;To use the above method for sorting an array one would simply use&amp;nbsp;&lt;font face="Courier New" size="2"&gt;&lt;font color="#008080"&gt;Array&lt;/font&gt;.Sort(myStringArray, CompareNaturally);&lt;/font&gt; where &lt;font face="Courier New" size="2"&gt;myStringArray&lt;/font&gt; is of course an array of strings. Wilco recommended I should use &lt;font face="Courier New" size="2"&gt;&lt;font color="#008080"&gt;Regex&lt;/font&gt;.Replace()&lt;/font&gt; and &lt;font face="Courier New" size="2"&gt;&lt;font color="#008080"&gt;MatchEvaluator&lt;/font&gt;&lt;/font&gt; to make it even shorter. So, here is a one-liner (okay, it's split on multiple lines but just for easier reading):&lt;/p&gt;&lt;pre&gt;&lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; CompareNaturally_Short(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; a, &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; b)
{
    &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;font color="#008080"&gt;Regex&lt;/font&gt;.Replace(a ?? &lt;span style="color: #8b0000"&gt;""&lt;/span&gt;, &lt;span style="color: #8b0000"&gt;@"\d+|[^\d]+"&lt;/span&gt;, &lt;span style="color: #0000ff"&gt;delegate&lt;/span&gt;(&lt;font color="#008080"&gt;Match&lt;/font&gt; m) {
        &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;font color="#008080"&gt;Char&lt;/font&gt;.IsDigit(m.Value[0]) ? m.Value.PadLeft(
            &lt;font color="#008080"&gt;Math&lt;/font&gt;.Max(a.Length, b != &lt;span style="color: #0000ff"&gt;null&lt;/span&gt; ? b.Length : 0), &lt;span style="color: #8b0000"&gt;'0'&lt;/span&gt;) : m.Value; 
    }).CompareTo(&lt;font color="#008080"&gt;Regex&lt;/font&gt;.Replace(b, &lt;span style="color: #8b0000"&gt;@"\d+|[^\d]+"&lt;/span&gt;, &lt;span style="color: #0000ff"&gt;delegate&lt;/span&gt;(&lt;font color="#008080"&gt;Match&lt;/font&gt; m) {
        &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;font color="#008080"&gt;Char&lt;/font&gt;.IsDigit(m.Value[0]) ? m.Value.PadLeft(
            &lt;font color="#008080"&gt;Math&lt;/font&gt;.Max(a != &lt;span style="color: #0000ff"&gt;null&lt;/span&gt; ? a.Length : 0, b.Length), &lt;span style="color: #8b0000"&gt;'0'&lt;/span&gt;) : m.Value; 
    }));
}&lt;/pre&gt;The idea behind this shorter version is pretty much the same with the exception that the &lt;font face="Courier New" size="2"&gt;&lt;font color="#008080"&gt;MatchEvaluator&lt;/font&gt;&lt;/font&gt; takes care of padding the numeric strings. Don't hesitate contacting me or dropping a comment if you spot errors in either of the methods&amp;nbsp;- Unfortunately I really didn't have time to test them thoroughly with different type of inputs.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2867299807863618924-2501986976434518713?l=demoeffect.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://demoeffect.blogspot.com/feeds/2501986976434518713/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2867299807863618924&amp;postID=2501986976434518713' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2867299807863618924/posts/default/2501986976434518713'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2867299807863618924/posts/default/2501986976434518713'/><link rel='alternate' type='text/html' href='http://demoeffect.blogspot.com/2007/12/natural-sort-in-c.html' title='Natural Sort in C#'/><author><name>teemu</name><uri>http://www.blogger.com/profile/15705736457443484192</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2867299807863618924.post-6767003222710045593</id><published>2007-01-06T19:59:00.001+02:00</published><updated>2007-03-18T15:06:09.769+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='ASP.NET'/><title type='text'>ASP.NET: Adding Style Property to Web Control</title><content type='html'>&lt;p&gt;Let's start off with something relatively easy. There are some web controls in ASP.NET (Calendar, TreeView and Menu for instance) that allow you to change various aspects of their looks with style properties.&lt;/p&gt;&lt;p&gt;There are two ways to set a style property in .aspx file. First you can set the property as an attribute to the element that defines the control by concatenating the name of the style object with the property:&lt;/p&gt;&lt;pre style="MARGIN-BOTTOM: 25px"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#c71585;"&gt;&lt;span style="color:#800000;"&gt;asp&lt;/span&gt;&lt;/span&gt;:&lt;span style="color:#800000;"&gt;TreeView&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;ID&lt;/span&gt;=&lt;span style="color:#0000ff;"&gt;"TreeView1"&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;runat&lt;/span&gt;=&lt;span style="color:#0000ff;"&gt;"server"&lt;/span&gt;
    &lt;span style="color:#ff0000;"&gt;NodeStyle&lt;/span&gt;-&lt;span style="color:#ff0000;"&gt;CssClass&lt;/span&gt;=&lt;span style="color:#0000ff;"&gt;"treeViewNode"&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;p&gt;Another way is to write the style as its own element that is placed inside the element defining the control:&lt;/p&gt;&lt;pre style="MARGIN-BOTTOM: 25px"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#c71585;"&gt;&lt;span style="color:#800000;"&gt;asp&lt;/span&gt;&lt;/span&gt;:&lt;span style="color:#800000;"&gt;TreeView&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;ID&lt;/span&gt;=&lt;span style="color:#0000ff;"&gt;"TreeView1"&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;runat&lt;/span&gt;=&lt;span style="color:#0000ff;"&gt;"server"&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;NodeStyle&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;CssClass&lt;/span&gt;=&lt;span style="color:#0000ff;"&gt;"treeViewNode"&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#c71585;"&gt;&lt;span style="color:#800000;"&gt;asp&lt;/span&gt;&lt;/span&gt;:&lt;span style="color:#800000;"&gt;TreeView&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;p&gt;Naturally the style can be defined in codebehind as well:&lt;/p&gt;&lt;pre style="MARGIN-BOTTOM: 25px"&gt;TreeView1.NodeStyle.CssClass = "&lt;span style="color:#8b0000;"&gt;treeViewNode&lt;/span&gt;";&lt;/pre&gt;&lt;p&gt;Now, what if you want to add your own style property to your custom web control? The task is not that difficult but there are few things that need to be taken into consideration. First style object is what they call a complex object so writing a simple setter/getter property for the style in your custom control class doesn't cut it. Secondly you might want to store the style object to view state if you're going to change the style at some point and have the change persist.&lt;/p&gt;&lt;p&gt;We'll start off by adding an instance variable for the style object to the class:&lt;/p&gt;&lt;pre style="MARGIN-BOTTOM: 25px"&gt;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#008080;"&gt;Style&lt;/span&gt; myCustomStyle = &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;;&lt;/pre&gt;&lt;p&gt;Next we will add a property for the style:&lt;/p&gt;&lt;pre style="MARGIN-BOTTOM: 25px"&gt;[&lt;span style="color:#008080;"&gt;DesignerSerializationVisibility&lt;/span&gt;(&lt;span style="color:#008080;"&gt;DesignerSerializationVisibility&lt;/span&gt;.Content),
&lt;span style="color:#008080;"&gt;PersistenceMode&lt;/span&gt;(&lt;span style="color:#008080;"&gt;PersistenceMode&lt;/span&gt;.InnerProperty)]
&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#008080;"&gt;Style&lt;/span&gt; MyCustomStyle
{
    &lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;
    {
        &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (myCustomStyle == &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;)
        {
            myCustomStyle = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#008080;"&gt;Style&lt;/span&gt;();
            &lt;span style="color:#008000;"&gt;// Set some default values here.&lt;/span&gt;

            &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (IsTrackingViewState)
                ((&lt;span style="color:#008080;"&gt;IStateManager&lt;/span&gt;)myCustomStyle).TrackViewState();
        }

        &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; myCustomStyle;
    }
}&lt;/pre&gt;&lt;p&gt;Notice how there is no setter part in the above property. The framework will call the getter and set properties directly to the object our getter provides. &lt;span style="font-family:Courier New;"&gt;TrackViewState()&lt;/span&gt; is called to notify the framework that our style object should be tracked and changes to it should be saved as part of the control view state. We also add &lt;span style="font-family:Courier New;"&gt;PersistenceMode(PersistenceMode.InnerProperty)&lt;/span&gt; to the property's metadata to specify that the property should persist as an inner property.&lt;/p&gt;&lt;p&gt;We are almost done but we still need to take care of view state. We want the view state for our style object to be loaded and saved at the same time with the control's view state. This is achieved by simply overloading the control's &lt;span style="font-family:Courier New;"&gt;LoadViewState()&lt;/span&gt;, &lt;span style="font-family:Courier New;"&gt;SaveViewState()&lt;/span&gt; and &lt;span style="font-family:Courier New;"&gt;TrackViewState()&lt;/span&gt; methods as follows:&lt;/p&gt;&lt;pre style="MARGIN-BOTTOM: 25px"&gt;&lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;override&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; LoadViewState(&lt;span style="color:#0000ff;"&gt;object&lt;/span&gt; state)
{
    &lt;span style="color:#0000ff;"&gt;object&lt;/span&gt;[] states = (&lt;span style="color:#0000ff;"&gt;object&lt;/span&gt;[])state;

    &lt;span style="color:#0000ff;"&gt;base&lt;/span&gt;.LoadViewState(states[0]);
    ((&lt;span style="color:#008080;"&gt;IStateManager&lt;/span&gt;)MyCustomStyle).LoadViewState(states[1]);
}

&lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;override&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;object&lt;/span&gt; SaveViewState()
{
    &lt;span style="color:#0000ff;"&gt;object&lt;/span&gt;[] states = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;object&lt;/span&gt;[2];

    states[0] = &lt;span style="color:#0000ff;"&gt;base&lt;/span&gt;.SaveViewState();
    states[1] = (myCustomStyle != &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;) ? ((&lt;span style="color:#008080;"&gt;IStateManager&lt;/span&gt;)myCustomStyle).SaveViewState() : &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;;

    &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; states;
}

&lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;override&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; TrackViewState()
{
    &lt;span style="color:#0000ff;"&gt;base&lt;/span&gt;.TrackViewState();

    &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (myCustomStyle != &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;)
        ((&lt;span style="color:#008080;"&gt;IStateManager&lt;/span&gt;)myCustomStyle).TrackViewState();
}&lt;/pre&gt;&lt;p&gt;That's it! Now you should be able to use your style property just like with built-in ASP.NET controls. What you might still want to do is to apply the styles defined in the style object to the HTML code the control renders by overriding the control's &lt;span style="font-family:Courier;"&gt;Render()&lt;/span&gt; method.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Update (18 Mar 2007):&lt;/strong&gt; I had a small typo in &lt;span style="font-family:Courier New;"&gt;LoadViewState()&lt;/span&gt; method. Instead of accessing the &lt;span style="font-family:Courier New;"&gt;myCustomStyle&lt;/span&gt; instance variable in the method, the property &lt;span style="font-family:Courier New;"&gt;MyCustomStyle&lt;/span&gt; should have been accessed instead. Thanks to &lt;a href="http://zifiglio.blogspot.com/"&gt;kal-EL@EFNet&lt;/a&gt; for pointing that out.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2867299807863618924-6767003222710045593?l=demoeffect.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://demoeffect.blogspot.com/feeds/6767003222710045593/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2867299807863618924&amp;postID=6767003222710045593' title='32 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2867299807863618924/posts/default/6767003222710045593'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2867299807863618924/posts/default/6767003222710045593'/><link rel='alternate' type='text/html' href='http://demoeffect.blogspot.com/2007/01/aspnet-adding-style-property-to-web.html' title='ASP.NET: Adding Style Property to Web Control'/><author><name>teemu</name><uri>http://www.blogger.com/profile/15705736457443484192</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>32</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2867299807863618924.post-6408255685398561191</id><published>2007-01-05T20:16:00.001+02:00</published><updated>2007-01-06T13:49:59.735+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='General'/><title type='text'>So, Why Yet Another Blog?</title><content type='html'>&lt;p&gt;I'm very well aware that these days everybody and their mother has a blog and about 90 % of them are never read by anyone. The thing with this blog, though, is that I don't care at all whether someone reads it or not. I'm mainly going to use this as a place to gather some programming tips and tricks I've encountered so that I can access them quickly if I ever have the need. So, yeah, this is probably going to be a rather boring and geeky blog but fortunately no one is forced to read it (at least I hope so). However, if this blog actually manages to help someone then I've accomplished more than I originally expected.&lt;/p&gt; &lt;p&gt;I can't say I enjoy ranting about things but sometimes I just can't help myself. So don't be surprised if you occasionally find a blog entry that contains ranting and whining about something totally insignificant and unrelated. Consider yourself warned.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2867299807863618924-6408255685398561191?l=demoeffect.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://demoeffect.blogspot.com/feeds/6408255685398561191/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2867299807863618924&amp;postID=6408255685398561191' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2867299807863618924/posts/default/6408255685398561191'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2867299807863618924/posts/default/6408255685398561191'/><link rel='alternate' type='text/html' href='http://demoeffect.blogspot.com/2007/01/so-why-yet-another-blog.html' title='So, Why Yet Another Blog?'/><author><name>teemu</name><uri>http://www.blogger.com/profile/15705736457443484192</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
