diff options
author | Gert Driesen <drieseng@users.sourceforge.net> | 2007-07-21 13:15:36 +0400 |
---|---|---|
committer | Gert Driesen <drieseng@users.sourceforge.net> | 2007-07-21 13:15:36 +0400 |
commit | 5993c313005b7e8cec11869730b3f0526001734b (patch) | |
tree | 68c7b8515fe4a8ff8bbcce09619b3e213e136a34 /mcs/class | |
parent | 29389a715706be264255869cf52355498567d9e3 (diff) |
* InstanceDescriptor.cs: Allow null members. Fixed exception messages.
For properties, do not perform argument check. Fixed Invoke to return
null when member is null or when member is not ctor, method, field or
property (eg. a type). Removed obsolete HasThis method.
* InstanceDescriptorTest.cs: Improved ctor tests. Added tests for
properties and fields.
svn path=/trunk/mcs/; revision=82411
Diffstat (limited to 'mcs/class')
4 files changed, 219 insertions, 62 deletions
diff --git a/mcs/class/System/System.ComponentModel.Design.Serialization/ChangeLog b/mcs/class/System/System.ComponentModel.Design.Serialization/ChangeLog index 3fd1edffa81..6ffcb96cf92 100644 --- a/mcs/class/System/System.ComponentModel.Design.Serialization/ChangeLog +++ b/mcs/class/System/System.ComponentModel.Design.Serialization/ChangeLog @@ -1,3 +1,10 @@ +2007-07-21 Gert Driesen <drieseng@users.sourceforge.net> + + * InstanceDescriptor.cs: Allow null members. Fixed exception messages. + For properties, do not perform argument check. Fixed Invoke to return + null when member is null or when member is not ctor, method, field or + property (eg. a type). Removed obsolete HasThis method. + 2007-07-18 Ivan N. Zlatev <contact@i-nz.net> * InstanceDescriptor.cs: Fix a NRE. diff --git a/mcs/class/System/System.ComponentModel.Design.Serialization/InstanceDescriptor.cs b/mcs/class/System/System.ComponentModel.Design.Serialization/InstanceDescriptor.cs index ade245b313a..392ba758921 100644 --- a/mcs/class/System/System.ComponentModel.Design.Serialization/InstanceDescriptor.cs +++ b/mcs/class/System/System.ComponentModel.Design.Serialization/InstanceDescriptor.cs @@ -33,8 +33,8 @@ using System.Collections; using System.Reflection; using System.Security.Permissions; -namespace System.ComponentModel.Design.Serialization { - +namespace System.ComponentModel.Design.Serialization +{ [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)] public sealed class InstanceDescriptor { @@ -50,24 +50,27 @@ namespace System.ComponentModel.Design.Serialization { public InstanceDescriptor(MemberInfo member, ICollection arguments, bool isComplete) { this.isComplete = isComplete; - if ((member != null) && !IsMemberValid (member, arguments)) - throw new ArgumentException ("Only Constructor, Method, Field or Property members allowed", "member"); + ValidateMember (member, arguments); this.member = member; this.arguments = arguments; } - private bool IsMemberValid (MemberInfo member, ICollection arguments) + private void ValidateMember (MemberInfo member, ICollection arguments) { + if (member == null) + return; + switch (member.MemberType) { - // According to docs only these types are allowed + // According to docs only these types are allowed, but the docs do + // state what happens for other types case MemberTypes.Constructor: ConstructorInfo CI = (ConstructorInfo) member; if (arguments == null) // null counts as no arguments if (CI.GetParameters().Length != 0) - throw new ArgumentException ("Invalid number of arguments for this constructor", "arguments"); + throw new ArgumentException ("Invalid number of arguments for this constructor"); if (arguments.Count != CI.GetParameters().Length) - throw new ArgumentException ("Invalid number of arguments for this constructor", "arguments"); - return true; + throw new ArgumentException ("Invalid number of arguments for this constructor"); + break; case MemberTypes.Method: MethodInfo MI = (MethodInfo) member; if (!MI.IsStatic) @@ -76,32 +79,24 @@ namespace System.ComponentModel.Design.Serialization { if (MI.GetParameters().Length != 0) throw new ArgumentException ("Invalid number of arguments for this method", "arguments"); if (arguments.Count != MI.GetParameters().Length) - throw new ArgumentException ("Invalid number of arguments for this method", "arguments"); - return true; + throw new ArgumentException ("Invalid number of arguments for this method"); + break; case MemberTypes.Field: FieldInfo FI = (FieldInfo) member; if (!FI.IsStatic) - throw new ArgumentException ("InstanceDescriptor only describes static (VB.Net: shared) members", "member"); - if (arguments == null) // null counts as no arguments - return true; - if (arguments.Count == 0) - throw new ArgumentException ("Field members do not take any arguments", "arguments"); - return true; + throw new ArgumentException ("Parameter must be static"); + if (arguments != null && arguments.Count != 0) // null counts as no arguments + throw new ArgumentException ("Field members do not take any arguments"); + break; case MemberTypes.Property: PropertyInfo PI = (PropertyInfo) member; if (!(PI.CanRead)) - throw new ArgumentException ("That property cannot be read", "member"); + throw new ArgumentException ("Parameter must be readable"); MethodInfo PIM = PI.GetGetMethod(); if (!PIM.IsStatic) - throw new ArgumentException ("InstanceDescriptor only describes static (VB.Net: shared) members", "member"); - if (arguments == null) // null counts as no arguments - if (PIM.GetParameters().Length != 0) - throw new ArgumentException ("Invalid number of arguments for this property", "arguments"); - if (arguments != null && arguments.Count != PIM.GetParameters().Length) - throw new ArgumentException ("Invalid number of arguments for this property", "arguments"); - return true; + throw new ArgumentException ("Parameter must be static"); + break; } - return false; } public ICollection Arguments { @@ -121,36 +116,17 @@ namespace System.ComponentModel.Design.Serialization { get { return member; } } - private bool HasThis () - { - if (member is ConstructorInfo) - return false; - MethodInfo mi = (member as MethodInfo); - if (mi != null) - return !mi.IsStatic; - FieldInfo fi = (member as FieldInfo); - if (fi != null) - return !fi.IsStatic; - PropertyInfo pi = (member as PropertyInfo); - if (pi != null) - return !pi.GetGetMethod ().IsStatic; - return true; - } - public object Invoke() { + if (member == null) + return null; + object[] parsearguments; if (arguments == null) parsearguments = new object[0]; - else if (HasThis ()) { - parsearguments = new object[arguments.Count - 1]; + else { + parsearguments = new object[arguments.Count]; arguments.CopyTo (parsearguments, 0); - } else { - parsearguments = (arguments as object[]); - if (parsearguments == null) { - parsearguments = new object[arguments.Count]; - arguments.CopyTo (parsearguments, 0); - } } //MemberInfo member; diff --git a/mcs/class/System/Test/System.ComponentModel.Design.Serialization/ChangeLog b/mcs/class/System/Test/System.ComponentModel.Design.Serialization/ChangeLog index 7255bbf1a37..b90ad03a6b5 100644 --- a/mcs/class/System/Test/System.ComponentModel.Design.Serialization/ChangeLog +++ b/mcs/class/System/Test/System.ComponentModel.Design.Serialization/ChangeLog @@ -1,3 +1,8 @@ +2007-07-21 Gert Driesen <drieseng@users.sourceforge.net> + + * InstanceDescriptorTest.cs: Improved ctor tests. Added tests for + properties and fields. + 2005-10-18 Sebastien Pouliot <sebastien@ximian.com> * InstanceDescriptorCas.cs: Added more tests for LinkDemand as it just diff --git a/mcs/class/System/Test/System.ComponentModel.Design.Serialization/InstanceDescriptorTest.cs b/mcs/class/System/Test/System.ComponentModel.Design.Serialization/InstanceDescriptorTest.cs index 9792ab64165..7fe4c29dbf3 100644 --- a/mcs/class/System/Test/System.ComponentModel.Design.Serialization/InstanceDescriptorTest.cs +++ b/mcs/class/System/Test/System.ComponentModel.Design.Serialization/InstanceDescriptorTest.cs @@ -34,6 +34,7 @@ using System.ComponentModel; using System.ComponentModel.Design.Serialization; using System.Globalization; using System.Reflection; +using System.Threading; namespace MonoTests.System.ComponentModel.Design.Serialization { @@ -50,20 +51,40 @@ namespace MonoTests.System.ComponentModel.Design.Serialization { } [Test] - public void Constructor_Null_ICollection () + public void Constructor0_Arguments_Mismatch () { - InstanceDescriptor id = new InstanceDescriptor (null, new object[] { }); - Assert.AreEqual (0, id.Arguments.Count, "Arguments"); - Assert.IsTrue (id.IsComplete, "IsComplete"); - Assert.IsNull (id.MemberInfo, "MemberInfo"); + try { + new InstanceDescriptor (ci, null); + Assert.Fail ("#1"); + } catch (ArgumentException ex) { + // Length mismatch + Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2"); + Assert.IsNull (ex.InnerException, "#B3"); + Assert.IsNotNull (ex.Message, "#B4"); + Assert.IsNull (ex.ParamName, "#B5"); + } } [Test] - [ExpectedException (typeof (ArgumentException))] - public void Constructor_MemberInfo_Null () + public void Constructor0_MemberInfo_Type () { - new InstanceDescriptor (ci, null); - // mismatch for required parameters + Type type = typeof (Uri); + InstanceDescriptor id = new InstanceDescriptor (type, + new object [] { url }); + Assert.AreEqual (1, id.Arguments.Count, "#1"); + Assert.IsTrue (id.IsComplete, "#2"); + Assert.AreSame (type, id.MemberInfo, "#3"); + Assert.IsNull (id.Invoke (), "#4"); + } + + [Test] + public void Constructor_Null_ICollection () + { + InstanceDescriptor id = new InstanceDescriptor (null, new object[] { }); + Assert.AreEqual (0, id.Arguments.Count, "#1"); + Assert.IsTrue (id.IsComplete, "#2"); + Assert.IsNull (id.MemberInfo, "#3"); + Assert.IsNull (id.Invoke (), "#4"); } [Test] @@ -81,9 +102,10 @@ namespace MonoTests.System.ComponentModel.Design.Serialization { public void Constructor_Null_ICollection_Boolean () { InstanceDescriptor id = new InstanceDescriptor (null, new object[] { }, true); - Assert.AreEqual (0, id.Arguments.Count, "Arguments"); - Assert.IsTrue (id.IsComplete, "IsComplete"); - Assert.IsNull (id.MemberInfo, "MemberInfo"); + Assert.AreEqual (0, id.Arguments.Count, "#1"); + Assert.IsTrue (id.IsComplete, "#2"); + Assert.IsNull (id.MemberInfo, "#3"); + Assert.IsNull (id.Invoke (), "#4"); } [Test] @@ -104,5 +126,152 @@ namespace MonoTests.System.ComponentModel.Design.Serialization { Uri uri = (Uri) id.Invoke (); Assert.AreEqual (url, uri.AbsoluteUri, "Invoke"); } + + [Test] + public void Field_Arguments_Empty () + { + FieldInfo fi = typeof (Uri).GetField ("SchemeDelimiter"); + + InstanceDescriptor id = new InstanceDescriptor (fi, new object [0]); + Assert.AreEqual (0, id.Arguments.Count, "#1"); + Assert.IsTrue (id.IsComplete, "#2"); + Assert.AreSame (fi, id.MemberInfo, "#3"); + Assert.IsNotNull (id.Invoke (), "#4"); + } + + [Test] + public void Field_Arguments_Mismatch () + { + FieldInfo fi = typeof (Uri).GetField ("SchemeDelimiter"); + + try { + new InstanceDescriptor (fi, new object [] { url }); + Assert.Fail ("#1"); + } catch (ArgumentException ex) { + // Parameter must be static + Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2"); + Assert.IsNull (ex.InnerException, "#3"); + Assert.IsNotNull (ex.Message, "#4"); + Assert.IsNull (ex.ParamName, "#5"); + } + } + + [Test] + public void Field_Arguments_Null () + { + FieldInfo fi = typeof (Uri).GetField ("SchemeDelimiter"); + + InstanceDescriptor id = new InstanceDescriptor (fi, null); + Assert.AreEqual (0, id.Arguments.Count, "#1"); + Assert.IsTrue (id.IsComplete, "#2"); + Assert.AreSame (fi, id.MemberInfo, "#3"); + Assert.IsNotNull (id.Invoke (), "#4"); + } + + [Test] + public void Field_MemberInfo_NonStatic () + { + FieldInfo fi = typeof (InstanceField).GetField ("Name"); + + try { + new InstanceDescriptor (fi, null); + Assert.Fail ("#1"); + } catch (ArgumentException ex) { + // Parameter must be static + Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2"); + Assert.IsNull (ex.InnerException, "#3"); + Assert.IsNotNull (ex.Message, "#4"); + Assert.IsNull (ex.ParamName, "#5"); + } + } + + [Test] + public void Property_Arguments_Mismatch () + { + PropertyInfo pi = typeof (Thread).GetProperty ("CurrentPrincipal"); + + InstanceDescriptor id = new InstanceDescriptor (pi, new object [] { url }); + Assert.AreEqual (1, id.Arguments.Count, "#1"); + object [] arguments = new object [id.Arguments.Count]; + id.Arguments.CopyTo (arguments, 0); + Assert.AreSame (url, arguments [0], "#2"); + Assert.IsTrue (id.IsComplete, "#3"); + Assert.AreSame (pi, id.MemberInfo, "#4"); + try { + id.Invoke (); + Assert.Fail ("#5"); + } catch (TargetParameterCountException) { + } + } + + [Test] + public void Property_Arguments_Null () + { + PropertyInfo pi = typeof (Thread).GetProperty ("CurrentPrincipal"); + + InstanceDescriptor id = new InstanceDescriptor (pi, null); + Assert.AreEqual (0, id.Arguments.Count, "#1"); + Assert.IsTrue (id.IsComplete, "#2"); + Assert.AreSame (pi, id.MemberInfo, "#3"); + Assert.IsNotNull (id.Invoke (), "#4"); + } + + [Test] + public void Property_MemberInfo_NonStatic () + { + PropertyInfo pi = typeof (Uri).GetProperty ("Host"); + + try { + new InstanceDescriptor (pi, null); + Assert.Fail ("#A1"); + } catch (ArgumentException ex) { + // Parameter must be static + Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2"); + Assert.IsNull (ex.InnerException, "#A3"); + Assert.IsNotNull (ex.Message, "#A4"); + Assert.IsNull (ex.ParamName, "#A5"); + } + + try { + new InstanceDescriptor (pi, null, false); + Assert.Fail ("#B1"); + } catch (ArgumentException ex) { + // Parameter must be static + Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2"); + Assert.IsNull (ex.InnerException, "#B3"); + Assert.IsNotNull (ex.Message, "#B4"); + Assert.IsNull (ex.ParamName, "#B5"); + } + } + + [Test] + public void Property_MemberInfo_WriteOnly () + { + PropertyInfo pi = typeof (WriteOnlyProperty).GetProperty ("Name"); + + try { + new InstanceDescriptor (pi, null); + Assert.Fail ("#1"); + } catch (ArgumentException ex) { + // Parameter must be readable + Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2"); + Assert.IsNull (ex.InnerException, "#3"); + Assert.IsNotNull (ex.Message, "#4"); + Assert.IsNull (ex.ParamName, "#5"); + } + } + + class WriteOnlyProperty + { + public static string Name { + set { + } + } + } + + class InstanceField + { + public string Name; + } } } |