Browse Source

Fixed null reference exception when loading/generating code in the python forms designer when a control's property was null.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@4564 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Matt Ward 16 years ago
parent
commit
e9949d3692
  1. 24
      src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonControlFieldExpression.cs
  2. 4
      src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonPropertyValueAssignment.cs
  3. 10
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/PythonControlFieldExpressionTests.cs
  4. 6
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/PythonPropertyAssignmentToStringTests.cs

24
src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonControlFieldExpression.cs

@ -255,6 +255,19 @@ namespace ICSharpCode.PythonBinding
} }
return SetPropertyValue(component, memberName, propertyValue); return SetPropertyValue(component, memberName, propertyValue);
} }
/// <summary>
/// Converts the value to the property's type if required.
/// </summary>
public static object ConvertPropertyValue(PropertyDescriptor propertyDescriptor, object propertyValue)
{
if (propertyValue != null) {
if (!propertyDescriptor.PropertyType.IsAssignableFrom(propertyValue.GetType())) {
return propertyDescriptor.Converter.ConvertFrom(propertyValue);
}
}
return propertyValue;
}
/// <summary> /// <summary>
/// Gets the member object that matches the field member. /// Gets the member object that matches the field member.
@ -399,16 +412,5 @@ namespace ICSharpCode.PythonBinding
} }
return false; return false;
} }
/// <summary>
/// Converts the value to the property's type if required.
/// </summary>
static object ConvertPropertyValue(PropertyDescriptor propertyDescriptor, object propertyValue)
{
if (!propertyDescriptor.PropertyType.IsAssignableFrom(propertyValue.GetType())) {
return propertyDescriptor.Converter.ConvertFrom(propertyValue);
}
return propertyValue;
}
} }
} }

4
src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonPropertyValueAssignment.cs

@ -35,6 +35,10 @@ namespace ICSharpCode.PythonBinding
/// </remarks> /// </remarks>
public static string ToString(object propertyValue) public static string ToString(object propertyValue)
{ {
if (propertyValue == null) {
return "None";
}
Type propertyType = propertyValue.GetType(); Type propertyType = propertyValue.GetType();
if (propertyType == typeof(String)) { if (propertyType == typeof(String)) {
return GetQuotedString((string)propertyValue); return GetQuotedString((string)propertyValue);

10
src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/PythonControlFieldExpressionTests.cs

@ -6,6 +6,7 @@
// </file> // </file>
using System; using System;
using System.ComponentModel;
using System.Windows.Forms; using System.Windows.Forms;
using ICSharpCode.PythonBinding; using ICSharpCode.PythonBinding;
using IronPython.Compiler.Ast; using IronPython.Compiler.Ast;
@ -260,6 +261,15 @@ namespace PythonBinding.Tests.Designer
} }
} }
[Test]
public void NullPropertyValueConversion()
{
using (Form form = new Form()) {
PropertyDescriptor descriptor = TypeDescriptor.GetProperties(form).Find("Text", true);
Assert.IsNull(PythonControlFieldExpression.ConvertPropertyValue(descriptor, null));
}
}
void AssertAreEqual(PythonControlFieldExpression field, string variableName, string memberName, string methodName, string fullMemberName) void AssertAreEqual(PythonControlFieldExpression field, string variableName, string memberName, string methodName, string fullMemberName)
{ {
string expected = "Variable: " + variableName + " Member: " + memberName + " Method: " + methodName + " FullMemberName: " + fullMemberName; string expected = "Variable: " + variableName + " Member: " + memberName + " Method: " + methodName + " FullMemberName: " + fullMemberName;

6
src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/PythonPropertyAssignmentToStringTests.cs

@ -105,5 +105,11 @@ namespace PythonBinding.Tests.Designer
{ {
Assert.AreEqual("System.Windows.Forms.Cursors.Default", PythonPropertyValueAssignment.ToString(Cursors.Default)); Assert.AreEqual("System.Windows.Forms.Cursors.Default", PythonPropertyValueAssignment.ToString(Cursors.Default));
} }
[Test]
public void NullConversion()
{
Assert.AreEqual("None", PythonPropertyValueAssignment.ToString(null));
}
} }
} }

Loading…
Cancel
Save