No debugging in Silverlight 4 with VS2010?

If you get this nice error message, you are probably lost, because there is no hints on WHY!!!???

clip_image002

Microsoft Visual Studio
---------------------------
Unable to start debugging. The Silverlight Developer Runtime is not installed. Please install a matching version.

 

Check your registry!!

When you create a Silverlight project, VS looks for the HKLM,SOFTWARE\Microsoft\Silverlight\Components\Debugging reg key to make sure you have the dev runtime. 
And when you debug, Silverlight looks for the HKLM\Software\Microsoft\.NETFramework\DbgpackShimPath reg key.

Thanks Amy!! Check out her blog about Silverlight and web dev

 

So your registry should have those keys and pointing to

 

On 64bit machines check

HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Silverlight\Components\Debugging

image

HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\.NETFramework

image

Both these keys should point to the same version (see my examples for Silverlight RTM)

Unable to open project in Expression Blend (since you are using VS2010 with .NET 4)

If you get the following problem in Expression Blend 3.0.1927.0: “Unsupported project”

image
Figure: Error messages in Expression Blend

 

Add the following pieces manually to your project file (.proj)

<PropertyGroup Condition="'$(MSBuildToolsVersion)' == '3.5'">
  <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
</PropertyGroup>

and (not sure if that is actually needed…)

<ExpressionBlendVersion>3.0.1927.0</ExpressionBlendVersion>

 

image
Figure: Applied changes to the .proj file

 

Reason: Expression Blend is not .NET 4 aware, so it needs this switch at the top of the project file

Spot the bug … and avoid the bug with a unit test helper class

I have a class with the following code. This looks OK but isn’t…

 

    public class Lot : INotifyPropertyChanged
    {

        private string _actualHighestBidder;
        
       
        
        public string ActualHighestBidder
        {
            get
            {
                return _actualHighestBidder;
            }
            set
            {
                if (_actualHighestBidder.Equals(value) != true)
                {
                    _actualHighestBidder = value;
                    RaisePropertyChanged("ActualHighestBidder");
                }
            }
        }

     -- SNIP -- Implementation of INotifyPropertyChanged omitted

Spot the problem in the code above!!!

 

Scroll down

 

 

Scroll down

 

 

 

The private variable “_actualHighestBidder” is a string and is null.
Calling Equals on a null variable causes an annoying NullReferenceException.

 

 

There are 2 ways to solve this

1. Initialize your private backed field

        private string _actualHighestBidder = string.Empty;

2. Test for null

 public string ActualHighestBidder
        {
            get
            {
                return _actualHighestBidder;
            }
            set
            {
                if ((_actualHighestBidder == null || _actualHighestBidder.Equals(value) != true))
                {
                    _actualHighestBidder = value;
                    RaisePropertyChanged("ActualHighestBidder");
                }
            }
        }

I prefer the first method
but
how do we make sure we are not introducing this bugs in the future?

Note
This bug is very annoying, because we get this exception at runtime!

 

Idea!
image
Set all properties to some values with reflection

So that we can have a test like this

        [TestMethod()]
        public void LotProperties_SetAllProperties_NoException()
        {
            Lot target = new Lot();

            target.SetAllPublicProperties();
            target.GetAllPublicProperties();
            
            Assert.IsNotNull(target);
        }

 

My implementation of these 2 methods look like the below…

The method SetAllPublicProperties, sets all properties via Reflection
And the method GetAllPublicProperties does the same but tries to read the values!!

using System;
using System.Diagnostics;
using System.Reflection;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Tests.Integration.Utils
{
    public static class ReflectionHelper
    {



        public static void SetAllPublicProperties(this object target)
        {
            Type targetType = target.GetType();

            foreach (MemberInfo memberInfo in targetType.GetProperties())
            {
                //Debug.WriteLine("memberInfo: " + memberInfo.ToString());

                PropertyInfo propertyInfo = memberInfo as PropertyInfo;
                if (propertyInfo != null)
                {
                    //Debug.WriteLine("propertyInfo: field " + propertyInfo.ToString());

                    if (propertyInfo.CanWrite)
                    {
                        MethodInfo setMethod = propertyInfo.GetSetMethod();


                        if (propertyInfo.PropertyType == typeof(string))
                        {
                            setMethod.Invoke(target, new object[] { "String parameter" });

                        }
                        else if (propertyInfo.PropertyType == typeof(bool))
                        {
                            setMethod.Invoke(target, new object[] { true });
                        }

                        else if (propertyInfo.PropertyType == typeof(Single))
                        {
                            setMethod.Invoke(target, new object[] { 123 });
                        }
                        else if (propertyInfo.PropertyType == typeof(Single?))
                        {
                            setMethod.Invoke(target, new object[] { null });
                            setMethod.Invoke(target, new object[] { (Single?)123 });
                        }
                        else
                        {
                            Assert.Fail("Generic Poperty setter not implemented for type: " + propertyInfo.PropertyType);
                        }
                    }
                }
            }
        }



        public static void GetAllPublicProperties(this object target)
        {
            Type targetType = target.GetType();

            foreach (MemberInfo memberInfo in targetType.GetProperties()) //System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.
            {
                //Debug.WriteLine("memberInfo: " + memberInfo.ToString());

                PropertyInfo propertyInfo = memberInfo as PropertyInfo;
                if (propertyInfo != null)
                {
                    //Debug.WriteLine("propertyInfo: field " + propertyInfo.ToString());

                    if (propertyInfo.CanRead)
                    {
                        MethodInfo getMethod = propertyInfo.GetGetMethod();

                        object readValue = getMethod.Invoke(target, null);

                        Debug.WriteLine("Value from property: " + readValue);

                    }
                }
            }
        }
    }
}

 

Use it and extend it to all your needed types!

Latest Posts

Popular Posts