ObjectSpy.On

Display all property and field values for a given object
Language:
PowerShell
Keywords:
.Net
Code Snippet

Add-Type -Language CSharpVersion3 -UsingNamespace System.Linq -Name "ObjectSpy" -Namespace "SBrickey" -MemberDefinition @'
    public static string On(object o, string Prefix)
    {
        System.IO.StringWriter sw = new System.IO.StringWriter();

        var Members = o.GetType().GetMembers(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic |
                                             System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Static | 
                                             System.Reflection.BindingFlags.FlattenHierarchy |
                                             System.Reflection.BindingFlags.GetField | System.Reflection.BindingFlags.GetProperty);

        var Properties = Members.Where(m => m.MemberType == System.Reflection.MemberTypes.Property)
                                .Cast<System.Reflection.PropertyInfo>()
                                .Where(p => p.CanRead && !p.GetIndexParameters().Any())
                                .OrderBy(p => p.GetGetMethod() == null)
                                .ThenBy(p => p.GetGetMethod() != null && p.GetGetMethod().IsStatic);
        var Properties__Name__MaxLen = Properties.Max(p => p.Name.Length) + 2;
        var Properties__Type__MaxLen = Properties.Max(p => p.PropertyType.FullName.Length) + 2;
        var Properties_FormatString = String.Format("{{0}}{{1,-8}}{{2,-6}} {{3,-{0}}} {{4,-{1}}} = {{5}}", Properties__Type__MaxLen, Properties__Name__MaxLen);

        sw.WriteLine(String.Format("{0}--PROPERTIES--", Prefix));
        Properties.ToList()
                  .ForEach(p => sw.WriteLine(string.Format(Properties_FormatString,
                                                           Prefix,
                                                           p.GetGetMethod() != null && p.GetGetMethod().IsPublic
                                                           ? "public"
                                                           : "private",
                                                           p.GetGetMethod() != null && p.GetGetMethod().IsStatic ? "static" : "",
                                                           p.PropertyType.FullName,
                                                           p.Name,
                                                           p.GetValue(o, null)
                                                           )
                                            ) // sw.writeline
                          ); // foreach

        var Variables = Members.Where(m => m.MemberType == System.Reflection.MemberTypes.Field)
                               .Cast<System.Reflection.FieldInfo>()
                               .OrderBy(v => v.IsPublic)
                               .ThenBy(v => v.IsStatic);
        var Variables__Name__MaxLen = Variables.Max(v => v.Name.Length) + 2;
        var Variables__Type__MaxLen = Variables.Max(v => v.FieldType.FullName.Length) + 2;
        var Variables_FormatString = String.Format("{{0}}{{1,-8}}{{2,-6}} {{3,-{0}}} {{4,-{1}}} = {{5}}", Variables__Type__MaxLen, Variables__Name__MaxLen);
        sw.WriteLine(String.Format("{0}--VARIABLES--", Prefix));
        Variables.ToList()
                 .ForEach(v => sw.WriteLine(string.Format(Variables_FormatString,
                                                          Prefix,
                                                          v.IsPublic
                                                          ? "public"
                                                          : "private",
                                                          v.IsStatic ? "static" : "",
                                                          v.FieldType.FullName,
                                                          v.Name,
                                                          v.GetValue(o)
                                                          )
                                          ) // sw.writeline
                          ); // foreach

        sw.Flush();
        return sw.ToString();
    } // static string On(...)

'@

 

Example

$tSite = New-Object Microsoft.SharePoint.SPSite("http://SPServer/sites/sample")
$tWeb = $t2Site.OpenWeb("Docs")
[SBrickey.ObjectSpy]::On($t2Web, "")


Created 2011-10-21
comments powered by Disqus
Login