Skip to content

Commit

Permalink
Now the object dumper searches for the DumpAttribute.DumpMethod also …
Browse files Browse the repository at this point in the history
…in the class' DumpAttribute.

Fixed a bug in ProtocolExceptionToWebFaultResolver.
Added overload for WebException too.
  • Loading branch information
vmelamed committed Apr 14, 2016
1 parent cd244b2 commit 792c948
Show file tree
Hide file tree
Showing 25 changed files with 159 additions and 73 deletions.
2 changes: 2 additions & 0 deletions Aspects/Diagnostics/ClassMetadataRegistrar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//using System.Data;
//using System.Data.Metadata.Edm;
//using System.Data.SqlClient;
//using System.Net;
using System.Globalization;
using System.Linq.Expressions;
using System.Security;
Expand Down Expand Up @@ -50,6 +51,7 @@ public static ClassMetadataRegistrar RegisterMetadata()
//.Register<ValidationResult, ValidationResultDumpMetadata>()
//.Register<ValidationResults, ValidationResultsDumpMetadata>()
//.Register<ConfigurationErrorsException, ConfigurationErrorsExceptionDumpMetadata>()
//.Register<WebException, WebExceptionDumpMetadata>()
;
}

Expand Down
12 changes: 6 additions & 6 deletions Aspects/Diagnostics/DumpAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ public enum ShouldDump
/// instance itself - e.g. from a property containing the instance or explicitly passed to the <see cref="ObjectTextDumper.Dump"/>. In these
/// cases not <c>null</c> class applicable properties from the instance attribute take precedence over the class attribute properties.
/// </remarks>
[SuppressMessage("Microsoft.Design", "CA1019:DefineAccessorsForAttributeArguments", Justification="The display positional parameter is equal to Skip named parameter.")]
[SuppressMessage("Microsoft.Design", "CA1019:DefineAccessorsForAttributeArguments", Justification = "The display positional parameter is equal to Skip named parameter.")]
[AttributeUsage(
AttributeTargets.Struct|
AttributeTargets.Class |
AttributeTargets.Field |
AttributeTargets.Property, AllowMultiple=false, Inherited=false)]
AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public sealed class DumpAttribute : Attribute, IEquatable<DumpAttribute>, ICloneable
{
#region Constant instances.
Expand Down Expand Up @@ -267,7 +267,7 @@ public string ValueFormat
/// Gets or sets the name of the dump method in the class specified by <see cref="DumpClass"/>. The dump method implements custom formatting of the property's value.
/// The method must be static, public, have a return type of <see cref="T:String"/> and must take a single parameter of type or a base type of the property.
/// If the <see cref="DumpClass"/> is not specified then the <see cref="ObjectTextDumper"/> will look for a parameterless instance method by the same name in the
/// property's class or a static method with parameter the type or a base type of the property.
/// property's class or a static method with parameter the type or a base type of the property in the property's class, base class or the metadata class.
/// </summary>
/// <remarks>
/// Applicable to properties only.
Expand Down Expand Up @@ -392,8 +392,8 @@ public override int GetHashCode()
/// <c>true</c> if the objects are considered to be equal (<see cref="M:IEquatable.Equals{DumpAttribute}"/>);
/// otherwise <c>false</c>.
/// </returns>
public static bool operator==(DumpAttribute left, DumpAttribute right) => ReferenceEquals(left, null)
? ReferenceEquals(right, null)
public static bool operator ==(DumpAttribute left, DumpAttribute right) => ReferenceEquals(left, null)
? ReferenceEquals(right, null)
: left.Equals(right);

/// <summary>
Expand All @@ -405,7 +405,7 @@ public override int GetHashCode()
/// <c>true</c> if the objects are not considered to be equal (<see cref="M:IEquatable.Equals{DumpAttribute}"/>);
/// otherwise <c>false</c>.
/// </returns>
public static bool operator!=(DumpAttribute left, DumpAttribute right) => !(left==right);
public static bool operator !=(DumpAttribute left, DumpAttribute right) => !(left==right);
#endregion
}
}
42 changes: 42 additions & 0 deletions Aspects/Diagnostics/ExternalMetadata/WebExceptionDumpMetadata.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.IO;
using System.Net;

namespace vm.Aspects.Diagnostics.ExternalMetadata
{
/// <remarks/>
public abstract class WebExceptionDumpMetadata
{
/// <remarks/>
[Dump(0)]
public object Status;

/// <remarks/>
[Dump(1, DumpClass = typeof(WebExceptionDumpMetadata), DumpMethod = nameof(DumpResponse))]
public object Response;

/// <remarks/>
public static string DumpResponse(
WebResponse response)
{
var stream = response?.GetResponseStream();

if (stream == null)
return "<null>";

try
{
var reader = new StreamReader(stream, true);

var result = reader.ReadToEnd();
stream.Seek(0, SeekOrigin.Begin);

return result;
}
catch (Exception)
{
return "<null>";
}
}
}
}
4 changes: 2 additions & 2 deletions Aspects/Diagnostics/NuGet/ObjectDumper.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>AspectObjectDumper</id>
<version>1.5.3</version>
<version>1.5.4</version>
<authors>Val Melamed</authors>
<owners>Val Melamed</owners>
<summary>
Expand All @@ -28,7 +28,7 @@
* Build and tested with .NET 4.0, 4.6. This package targets .NET 4.0.
</description>
<releaseNotes>
Added contracts to the internal DumpTextWriter.
Now the dumper searches for the DumpAttribute.DumpMethod also in the class' DumpAttribute.
</releaseNotes>
<licenseUrl>https://aspectobjectdumper.codeplex.com/license</licenseUrl>
<projectUrl>https://aspectobjectdumper.codeplex.com/</projectUrl>
Expand Down
7 changes: 4 additions & 3 deletions Aspects/Diagnostics/NuGet/PublishObjectDumper.cmd
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
pushd
cd %~dp0..
del ..\*.nupkg
del *.nupkg
NuGet Update -self
call "%VS140COMNTOOLS%vsvars32.bat"
if not .%1.==.. NuGet SetApiKey %1
msbuild vm.Aspects.Diagnostics.ObjectDumper.csproj /t:Rebuild /p:Configuration=Release /m
if errorlevel 1 goto exit
NuGet Pack NuGet\ObjectDumper.nuspec -Prop Configuration=Release -symbols
if errorlevel 1 goto exit
@echo Press any key to push to NuGet... > con:
if exist c:\NuGet copy /y *.nupkg c:\NuGet
@echo Press any key to push to NuGet.org... > con:
@pause > nul:
NuGet Push AspectObjectDumper.1.5.3.nupkg
NuGet Push AspectObjectDumper.1.5.4.nupkg
:exit
popd
pause
54 changes: 29 additions & 25 deletions Aspects/Diagnostics/ObjectTextDumper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public sealed partial class ObjectTextDumper : IDisposable
/// <summary>
/// The default binding flags determining which properties to be dumped
/// </summary>
[SuppressMessage("Microsoft.Naming", "CA1726:UsePreferredTerms", MessageId="Flags")]
[SuppressMessage("Microsoft.Naming", "CA1726:UsePreferredTerms", MessageId = "Flags")]
public static BindingFlags DefaultPropertiesBindingFlags
{
get { return defaultPropertiesBindingFlags; }
Expand All @@ -96,7 +96,7 @@ public static BindingFlags DefaultPropertiesBindingFlags
/// <summary>
/// The default binding flags determining which fields to be dumped
/// </summary>
[SuppressMessage("Microsoft.Naming", "CA1726:UsePreferredTerms", MessageId="Flags")]
[SuppressMessage("Microsoft.Naming", "CA1726:UsePreferredTerms", MessageId = "Flags")]
public static BindingFlags DefaultFieldsBindingFlags
{
get { return defaultFieldsBindingFlags; }
Expand All @@ -115,8 +115,8 @@ public static BindingFlags DefaultFieldsBindingFlags
/// <param name="propertiesBindingFlags">The binding flags of the properties.</param>
/// <param name="fieldsBindingFlags">The binding flags of the fields.</param>
/// <exception cref="System.ArgumentNullException">Thrown if <paramref name="writer" /> is <c>null</c>.</exception>
[SuppressMessage("Microsoft.Naming", "CA1726:UsePreferredTerms", MessageId="Flags")]
[SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId="0")]
[SuppressMessage("Microsoft.Naming", "CA1726:UsePreferredTerms", MessageId = "Flags")]
[SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0")]
public ObjectTextDumper(
TextWriter writer,
int indentLevel = 0,
Expand Down Expand Up @@ -471,7 +471,7 @@ void DumpProperty(
Contract.Requires(state!=null);

// should we dump it at all?
if (!state.CurrentProperty.CanRead() ||
if (!state.CurrentProperty.CanRead() ||
state.CurrentPropertyDumpAttribute.Skip == ShouldDump.Skip)
return;

Expand Down Expand Up @@ -504,7 +504,7 @@ void DumpProperty(

try
{
value = pi != null
value = pi != null
? pi.GetValue(state.Instance, null)
: fi.GetValue(state.Instance);
}
Expand All @@ -515,7 +515,7 @@ void DumpProperty(
}

// should we dump a null value of the current property
if (value == null &&
if (value == null &&
(state.CurrentPropertyDumpAttribute.DumpNullValues==ShouldDump.Skip ||
state.CurrentPropertyDumpAttribute.DumpNullValues==ShouldDump.Default && state.DumpNullValues==ShouldDump.Skip))
return;
Expand Down Expand Up @@ -554,12 +554,12 @@ void DumpProperty(
DumpObjectOfNonBasicValue(
value,
null,
state.CurrentPropertyDumpAttribute.IsDefaultAttribute()
state.CurrentPropertyDumpAttribute.IsDefaultAttribute()
? null
: state.CurrentPropertyDumpAttribute);
}

[SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity", Justification="It's OK.")]
[SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity", Justification = "It's OK.")]
bool DumpedPropertyCustom(
DumpState state,
object value,
Expand All @@ -585,17 +585,17 @@ bool DumpedPropertyCustom(
return false;

if (string.IsNullOrWhiteSpace(dumpMethodName))
dumpMethodName = nameof(Dump);
dumpMethodName = "Dump";

MethodInfo dumpMethod = null; // best match
MethodInfo dumpMethod2 = null; // second best

// try external class if specified
// try the external class if specified
if (dumpClass != null)
{
foreach (var mi in dumpClass.GetMethods(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy)
.Where(mi => mi.Name == dumpMethodName &&
mi.ReturnType == typeof(string) &&
.Where(mi => mi.Name == dumpMethodName &&
mi.ReturnType == typeof(string) &&
mi.GetParameters().Count() == 1))
{
if (mi.GetParameters()[0].ParameterType == type)
Expand All @@ -622,14 +622,18 @@ bool DumpedPropertyCustom(
return true;
}

// try the property's class or base class
// try the property's class or base class, or metadata class
foreach (var mi in type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy)
.Where(mi => mi.Name == dumpMethodName &&
.Where(mi => mi.Name == dumpMethodName &&
mi.ReturnType == typeof(string))
.Union(type.GetMethods(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy)
.Where(mi => mi.Name == dumpMethodName &&
mi.ReturnType == typeof(string) &&
mi.GetParameters().Count() == 1)))
.Union(type.GetMethods(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy)
.Where(mi => mi.Name == dumpMethodName &&
mi.ReturnType == typeof(string) &&
mi.GetParameters().Count() == 1))
.Union(state.ClassDumpData.Metadata.GetMethods(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy)
.Where(mi => mi.Name == dumpMethodName &&
mi.ReturnType == typeof(string) &&
mi.GetParameters().Count() == 1)))
{
// found an instance method
if (!mi.IsStatic && mi.GetParameters().Count() == 0)
Expand All @@ -644,7 +648,7 @@ bool DumpedPropertyCustom(
dumpMethod = mi;
else
if (mi.GetParameters()[0].ParameterType.IsAssignableFrom(type))
dumpMethod2 = mi;
dumpMethod2 = mi;
}
}

Expand Down Expand Up @@ -746,8 +750,8 @@ bool DumpedDelegate(object value)
d.Method.DeclaringType!=null ? d.Method.DeclaringType.Namespace : string.Empty,
d.Method.DeclaringType!=null ? d.Method.DeclaringType.AssemblyQualifiedName : string.Empty,
d.Method.Name,
d.Target==null
? Resources.ClassMethodDesignator
d.Target==null
? Resources.ClassMethodDesignator
: Resources.InstanceMethodDesignator);

return true;
Expand Down Expand Up @@ -810,7 +814,7 @@ bool DumpedSequenceObject(
max = int.MaxValue;
else
if (max == 0) // limit sequences of primitive types (can be very big)
max = DumpAttribute.DefaultMaxElements;
max = DumpAttribute.DefaultMaxElements;

if (sequenceType == typeof(byte[]))
{
Expand Down Expand Up @@ -895,7 +899,7 @@ static string GetTypeName(

string typeName = type.Name;

if (typeName.Length > 65 &&
if (typeName.Length > 65 &&
_hexadecimalSuffix.IsMatch(typeName.Substring(typeName.Length - 65-1)))
typeName = type.BaseType.Name.Substring(0, typeName.Length-65);

Expand Down Expand Up @@ -945,7 +949,7 @@ static string GetTypeName(
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
/// <remarks>Invokes the protected virtual <see cref="Dispose(bool)"/>.</remarks>
[SuppressMessage("Microsoft.Design", "CA1063:ImplementIDisposableCorrectly", Justification="It is correct.")]
[SuppressMessage("Microsoft.Design", "CA1063:ImplementIDisposableCorrectly", Justification = "It is correct.")]
public void Dispose()
{
// if it is disposed or in a process of disposing - return.
Expand Down
6 changes: 3 additions & 3 deletions Aspects/Diagnostics/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

[assembly: AssemblyTitle("vm.Aspects.Diagnostics.ObjectDumper")]
[assembly: AssemblyDescription("Dumps the properties' and fields' values of any .NET object in a text form.")]
[assembly: AssemblyVersion("1.5.3")]
[assembly: AssemblyFileVersion("1.5.3")]
[assembly: AssemblyInformationalVersion("1.5.3")]
[assembly: AssemblyVersion("1.5.4")]
[assembly: AssemblyFileVersion("1.5.4")]
[assembly: AssemblyInformationalVersion("1.5.4")]

[assembly: System.Runtime.CompilerServices.InternalsVisibleTo(
"vm.Aspects.Diagnostics.ObjectDumper.Tests, " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@
<Compile Include="ExternalMetadata\UpdateExceptionDumpMetadata.cs" />
<Compile Include="ExternalMetadata\ValidationResultDumpMetadata.cs" />
<Compile Include="ExternalMetadata\ValidationResultsDumpMetadata.cs" />
<Compile Include="ExternalMetadata\WebExceptionDumpMetadata.cs" />
<Compile Include="GlobalSuppressions.cs" />
<Compile Include="IMemberInfoComparer.cs" />
<Compile Include="ObjectTextDumper.BasicTypes.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>AspectExpressionSerialization</id>
<version>1.0.46</version>
<version>1.0.48</version>
<authors>Val Melamed</authors>
<owners>Val Melamed</owners>
<summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
[assembly: AssemblyTitle("vm.Aspects.Linq.Expressions.Serialization")]
[assembly: AssemblyDescription("Serializes and deserializes LINQ expression trees to and from XML documents.")]

[assembly: AssemblyVersion("1.0.46")]
[assembly: AssemblyFileVersion("1.0.46")]
[assembly: AssemblyInformationalVersion("1.0.46")]
[assembly: AssemblyVersion("1.0.48")]
[assembly: AssemblyFileVersion("1.0.48")]
[assembly: AssemblyInformationalVersion("1.0.48")]

[assembly: InternalsVisibleTo(
"vm.Aspects.Linq.Expressions.Serialization.Test, " +
Expand Down
6 changes: 3 additions & 3 deletions Aspects/Model/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
[assembly: AssemblyTitle("vm.Aspect.Model")]
[assembly: AssemblyDescription("Defines the IRepository and related base classes and utilities - a framework of building domain object model.")]

[assembly: AssemblyVersion("1.0.46")]
[assembly: AssemblyFileVersion("1.0.46")]
[assembly: AssemblyInformationalVersion("1.0.46")]
[assembly: AssemblyVersion("1.0.48")]
[assembly: AssemblyFileVersion("1.0.48")]
[assembly: AssemblyInformationalVersion("1.0.48")]

[assembly: System.Runtime.CompilerServices.InternalsVisibleTo(
"vm.Aspects.Model.Tests, " +
Expand Down
2 changes: 1 addition & 1 deletion Aspects/Model/Tests/packages.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="AspectObjectDumper" version="1.5.3" targetFramework="net46" />
<package id="AspectObjectDumper" version="1.5.4" targetFramework="net46" />
<package id="CommonServiceLocator" version="1.3" targetFramework="net46" />
<package id="EnterpriseLibrary.Common" version="6.0.1304.0" targetFramework="net46" />
<package id="EnterpriseLibrary.Logging" version="6.0.1304.0" targetFramework="net46" />
Expand Down
2 changes: 1 addition & 1 deletion Aspects/Model/packages.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="AspectObjectDumper" version="1.5.3" targetFramework="net46" />
<package id="AspectObjectDumper" version="1.5.4" targetFramework="net46" />
<package id="CommonServiceLocator" version="1.3" targetFramework="net46" />
<package id="EnterpriseLibrary.Common" version="6.0.1304.0" targetFramework="net46" />
<package id="EnterpriseLibrary.Validation" version="6.0.1304.0" targetFramework="net46" />
Expand Down
5 changes: 3 additions & 2 deletions Aspects/NuGet/PublishAspects.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ if errorlevel 1 goto exit
cd ..
NuGet Pack NuGet\vm.Aspects.nuspec -symbols -Prop Configuration=Release
if errorlevel 1 goto exit
@echo Press any key to push to NuGet... > con:
if exist c:\NuGet copy /y *.nupkg c:\NuGet
@echo Press any key to push to NuGet.org... > con:
@pause > nul:
NuGet Push vm.Aspects.1.0.46-beta.nupkg
NuGet Push vm.Aspects.1.0.48-beta.nupkg
:exit
popd
pause
Loading

0 comments on commit 792c948

Please sign in to comment.