LinqToNorthwind

23 篇文章 0 订阅

原文:http://code.msdn.microsoft.com/vstudio/LinqToNorthwind-875bafdb/sourcecode?fileId=46093&pathId=1173306596

ObjectDumper类:

using System;
using System.IO; 
using System.Collections; 
using System.Collections.Generic; 
using System.Reflection; 

namespace xxxxx
{
	public class ObjectDumper
	{
		public static void Write(object element) 
		{ 
			Write(element, 0); 
		} 

		public static void Write(object element, int depth) 
		{ 
			Write(element, depth, Console.Out); 
		} 

		public static void Write(object element, int depth, TextWriter log) 
		{ 
			ObjectDumper dumper = new ObjectDumper(depth); 
			dumper.writer = log; 
			dumper.WriteObject(null, element); 
		} 

		TextWriter writer; 
		int pos; 
		int level; 
		int depth; 

		private ObjectDumper(int depth) 
		{ 
			this.depth = depth; 
		} 

		private void Write(string s) 
		{ 
			if (s != null) { 
				writer.Write(s); 
				pos += s.Length; 
			} 
		} 

		private void WriteIndent() 
		{ 
			for (int i = 0; i < level; i++) writer.Write("  "); 
		} 

		private void WriteLine() 
		{ 
			writer.WriteLine(); 
			pos = 0; 
		} 

		private void WriteTab() 
		{ 
			Write("  "); 
			while (pos % 8 != 0) Write(" "); 
		} 

		private void WriteObject(string prefix, object element) 
		{ 
			if (element == null || element is ValueType || element is string) { 
				WriteIndent(); 
				Write(prefix); 
				WriteValue(element); 
				WriteLine(); 
			} 
			else { 
				IEnumerable enumerableElement = element as IEnumerable; 
				if (enumerableElement != null) { 
					foreach (object item in enumerableElement) { 
						if (item is IEnumerable && !(item is string)) { 
							WriteIndent(); 
							Write(prefix); 
							Write("..."); 
							WriteLine(); 
							if (level < depth) { 
								level++; 
								WriteObject(prefix, item); 
								level--; 
							} 
						} 
						else { 
							WriteObject(prefix, item); 
						} 
					} 
				} 
				else { 
					MemberInfo[] members = element.GetType().GetMembers(BindingFlags.Public | BindingFlags.Instance); 
					WriteIndent(); 
					Write(prefix); 
					bool propWritten = false; 
					foreach (MemberInfo m in members) { 
						FieldInfo f = m as FieldInfo; 
						PropertyInfo p = m as PropertyInfo; 
						if (f != null || p != null) { 
							if (propWritten) { 
								WriteTab(); 
							} 
							else { 
								propWritten = true; 
							} 
							Write(m.Name); 
							Write("="); 
							Type t = f != null ? f.FieldType : p.PropertyType; 
							if (t.IsValueType || t == typeof(string)) { 
								WriteValue(f != null ? f.GetValue(element) : p.GetValue(element, null)); 
							} 
							else { 
								if (typeof(IEnumerable).IsAssignableFrom(t)) { 
									Write("..."); 
								} 
								else { 
									Write("{ }"); 
								} 
							} 
						} 
					} 
					if (propWritten) WriteLine(); 
					if (level < depth) { 
						foreach (MemberInfo m in members) { 
							FieldInfo f = m as FieldInfo; 
							PropertyInfo p = m as PropertyInfo; 
							if (f != null || p != null) { 
								Type t = f != null ? f.FieldType : p.PropertyType; 
								if (!(t.IsValueType || t == typeof(string))) { 
									object value = f != null ? f.GetValue(element) : p.GetValue(element, null); 
									if (value != null) { 
										level++; 
										WriteObject(m.Name + ": ", value); 
										level--; 
									} 
								} 
							} 
						} 
					} 
				} 
			} 
		} 

		private void WriteValue(object o) 
		{ 
			if (o == null) { 
				Write("null"); 
			} 
			else if (o is DateTime) { 
				Write(((DateTime)o).ToShortDateString()); 
			} 
			else if (o is ValueType || o is string) { 
				Write(o.ToString()); 
			} 
			else if (o is IEnumerable) { 
				Write("..."); 
			} 
			else { 
				Write("{ }"); 
			} 
		} 
	}
}
eg: using System.Collections.Generic;

public void Linq34() 
		{ 
			string[] words = { "aPPLE", "AbAcUs", "bRaNcH", "BlUeBeRrY", "ClOvEr", "cHeRry" }; 
			var sortedWords = words.OrderByDescending(a => a, new CaseInsensitiveComparer()); 

			ObjectDumper.Write(sortedWords); 
		} 

		public class CaseInsensitiveComparer : IComparer<string> 
		{ 
			public int Compare(string x, string y) 
			{ 
				return string.Compare(x, y, StringComparison.OrdinalIgnoreCase); 
			} 
		}

LinqToNorthwind.zip下载


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值