jump to navigation

Copy and Paste Formatting with Visual Studio’s Dark Theme May 17, 2013

Posted by codinglifestyle in CodeProject, Visual Studio, Visual Studio 2012.
Tags: , , , , , ,
12 comments

I recently upgraded to VS2012 and, like most of you, was aghast at the default theme. Sure, after installing the updates the blue theme was good. But before updating I tried the dark theme… and I liked it! As our phones and tablets often use dark themes and websites are being remade to look like tablet apps the dark theme had a modern look to it and is easy on the eyes.

Look, it doesn’t matter what I think of the dark theme. I simply want to discuss an undesirable side effect affecting Copy & Paste (don’t forget Cut too). When copying code to an email, Word, or your IM window you realize the dark theme has a dark side:

What we get in the first paste attempt is a WYSIWYG copy of the formatting from Visual Studio. Clearly what most of us want is the second paste attempt. And the formatting issue can get even worse. When pasting to some programs you get white text on a white background. The problem is so obvious it seems the people who made the dark theme don’t actually use it day to day.

I was finding that I needed to: open options, select the blue theme, copy my code to the clipboard, open options again, and reselect the dark theme.

What a tedious workaround! So I went on a quest this morning and am happy to report I have a solution!

  1. Open Tools → Extensions and Updates
  2. Select Online (Visual Studio Gallery) and search for Productivity Power Tools 2012
  3. Download and restart Visual Studio when prompted
  4. Open Tools → Options
  5. Expand Productivity Power Tools and select HTML Copy

  1. Change the BeforeCodeSnippet option to:
    1. <style type=”text/css”>.identifier {color:black !important;}</style><pre style=”{font-family}{font-size}{font-weight}{font-style}”>
  2. Change EmitSpanClass to:
    1. True
  3. Check EmitSpanStyle is:
    1. True

You may want to optionally turn off all other features other than HTML Copy from the “All Extensions” menu.

Let’s take a look at what this feature is doing. When you copy text to the clipboard you can have multiple data formats such as Text, RTF, and HTML. When we’re pasting our code in to Word or an email it will typically use the HTML format (configuration dependent). Here is what we actually see in the clipboard when we do a copy after configuring VS as above.

</pre>
<!--StartFragment-->
<style type="text/css">
 .identifier {
 color: black !important;
 }
</style>
<pre style="font-family: Consolas; font-size: 13;"><span class="keyword" style="color:#569cd6;">var</span>&nbsp;<span class="identifier" style="color:white;">bob</span>&nbsp;<span class="operator" style="color:#b4b4b4;">=</span>&nbsp;<span class="keyword" style="color:#569cd6;">string</span><span class="operator" style="color:#b4b4b4;">.</span><span class="identifier" style="color:white;">Empty</span>;
</pre>
<!--EndFragment-->

Note that we’ve removed the background colour which means all of our identifier text is being set as white on a white background. However the style element is overriding the identifier color and setting it to black. This gives us the desired results:

var bob = string.Empty;

Advertisements

Using reflection to copy a DataRow to a class September 14, 2007

Posted by codinglifestyle in ASP.NET, C#.
Tags: , , , ,
add a comment

Very often when working with DB tables we serialize a row in to class.  This entity class simply holds the data and hosts a rack of get and set methods.  The data layer serializes this class, the business logic layer manipulated it, and the UI layer ultimately displays it.  Here is a simple example:

 

    class Class1

    {

        private string m_sProp1;

        private int m_nProp2;

 

        public string Prop1

        {

            get

            {

                return m_sProp1;

            }

            set

            {

                m_sProp1 = value;

            }

        }

 

        public int Prop2

        {

            get

            {

                return m_nProp2;

            }

            set

            {

                m_nProp2 = value;

            }

        }

    }

 

Very often somewhere in the UI and/or logic layers you have a large function laboriously copying each value in or out of the entity class.  In my case, I was serializing the entity to a DataSet to be displayed in a Repeater control.  So I thought to myself, “Self, wouldn’t it be cool to use reflection to automatically perform the copy”.  While I didn’t know much about reflection a couple hours ago, it’s remarkably easy to use and, as you probably know, very powerful.  Let’s get started…

 

I need a copy function to copy a DataRow to my entity class and vice versa.  We’ll address the first case. 

 

        public static object SetValuesToObject(Type tClass, DataRow dr)

        {

            try

            {

                Object oClass        = System.Activator.CreateInstance(tClass);

                MemberInfo[] methods = tClass.GetMethods();

                ArrayList aMethods   = new ArrayList();

                object[] aoParam     = new object[1];

 

                //Get simple SET methods

                foreach (MethodInfo method in methods)

                {

                    if (method.DeclaringType == tClass && method.Name.StartsWith(“set_”))

                        aMethods.Add(method);

                }

 

                //Invoke each set method with mapped value

                for (int i = 0; i < aMethods.Count; i++)

                {

                    try

                    {

                        MethodInfo mInvoke = (MethodInfo)aMethods[i];

                        //Remove “set_” from method name

                        string sColumn = mInvoke.Name.Remove(0, 4);

                        //If row contains value for method…

                        if (dr.Table.Columns.Contains(sColumn))

                        {

                            //Get the parameter (always one for a set property)

                            ParameterInfo[] api = mInvoke.GetParameters();

                            ParameterInfo   pi  = api[0]; 

 

                            //Convert value to parameter type

                            aoParam[0] = Convert.ChangeType(dr[sColumn], pi.ParameterType);

                           

                            //Invoke the method

                            mInvoke.Invoke(oClass, aoParam);

                        }

                    }

                    catch

                    {

                        System.Diagnostics.Debug.Assert(false, “SetValuesToObject failed to set a value to an object”);

                    }

                }

                return oClass;

            }

            catch

            {

                System.Diagnostics.Debug.Assert(false, “SetValuesToObject failed to create an object”);

                return null;

            }

        } 



The way this works is I’m going to pass in a Type and a DataRow.  First we look through at all the methods in the entity class and pull out all the set methods.  Then look at the DataRow to see if we have a column which corresponds to the method.  Looking at the example entity class above, I would be hoping to have a column called Prop1 and Prop2*.  We the look to see what type of set method is expecting: string, int, DateTime, ect.  We use this to convert the value from the DataRow and set this in to an object array of length 1.  Finally, invoke the set method passing in the object array.  The end result, a newly instantiated entity class pre-populated with the values from the DataRow.

 

* Although it’s rare to abide by a naming convention in reality, this is new code, so we will keep naming identical from DB to entity to DataTable.  It would be trivial to introduce a mapping of column to property.