C#.NET Class扩展 ToJson/Select/IsBetween/In/Compress/Serialize

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Data;
using System.Data.Common;
using System.Web.Script.Serialization;
using System.IO;
using System.Security.Cryptography;
using System.Runtime.Serialization.Formatters.Binary;

namespace Pub.Class {
public static class ClassExtensions {

public static string ToJson<TSource, TResult>(this TSource entity, Func<TSource, TResult> selector) {
List
<TSource> oblist = new List<TSource>();
oblist.Add(entity);
string str = oblist.Select(selector).ToJson();
return str.Substring(1, str.Length - 2);
}
public static TResult Select<TSource, TResult>(this TSource entity, Func<TSource, TResult> selector) {
List
<TSource> oblist = new List<TSource>();
oblist.Add(entity);
return oblist.Select(selector).ToList()[0];
}
public static bool EqualsAny<T>(this T obj, params T[] values) {
return (Array.IndexOf(values, obj) != -1);
}

public static bool IsBetween<T>(this T value, T minValue, T maxValue) where T : IComparable<T> {
return IsBetween(value, minValue, maxValue, null);
}
public static bool IsBetween<T>(this T value, T minValue, T maxValue, IComparer<T> comparer) where T : IComparable<T> {
comparer
= comparer ?? Comparer<T>.Default;

var minMaxCompare
= comparer.Compare(minValue, maxValue);
if(minMaxCompare < 0) {
return ((comparer.Compare(value, minValue) >= 0) && (comparer.Compare(value, maxValue) <= 0));
}
else if(minMaxCompare == 0) {
return (comparer.Compare(value, minValue) == 0);
}
else {
return ((comparer.Compare(value, maxValue) >= 0) && (comparer.Compare(value, minValue) <= 0));
}
}
public static bool In<T>(this T source, params T[] values) where T : IEquatable<T> {
if (values == null) return false;
return IEnumerableExtensions.In(source, ((IEnumerable<T>)values));
}

public static byte[] Compress<T>(this T data) where T : class {
return data.Serialize<T>().Compress();
}

public static T Clone<T>(this T o) where T : class {
Type type
= o.GetType().BaseType;

MethodInfo[] methodInfos
= type.GetMethods(BindingFlags.NonPublic
| BindingFlags.Instance
| BindingFlags.DeclaredOnly);
MethodInfo cloneMethod
= null;
foreach (var item in methodInfos) {
if (item.Name == "MemberwiseClone") {
cloneMethod
= item;
break;
}
}
if (cloneMethod != null) return (T)cloneMethod.Invoke(o, null);
return default(T);
}
public static byte[] Serialize<T>(this T o) where T : class {
Type type
= o.GetType();
if ((type.Attributes & TypeAttributes.Serializable) == 0)
throw new Exception("The Specified object must have [Serializable] attribute");

var formatter
= new BinaryFormatter();
using (MemoryStream ms = new MemoryStream()) {
formatter.Serialize(ms, o);
return ms.ToArray();
}
}

public static R Pipe<T, R>(this T o, Func<T, R> action) {
T buffer
= o;
return action(buffer);
}
public static T Pipe<T>(this T o, Action<T> action) {
T buffer
= o;
action(buffer);
return buffer;
}

public static bool IsDefault<T>(this T value) { return Equals(value, default(T)); }

//string s = "stringtest";
//Msg.Write(s.Compress<string>().Decompress<string>());
}
}

 

ToJson调用

 


private void TaskDetailLoad(){
int id = Request2.GetFInt("id", 0);
if (id <= 0) Msg.WriteEnd("{}");

Msg.WriteEnd(LP_TaskFactory.Instance().SelectByID(id).ToJson(p
=> new {
p.TaskID,
p.TaskName,
p.Quarter,
p.StartTime,
p.Intro,
Nodes
= LP_TaskDetailFactory.Instance().SelectListByTaskID(id, "", "", "", true)
.Select(z
=> new {
z.TaskDetailID,
z.DetailMonths,
z.DetailName,
z.DetailType,
z.DetailIntro,
z.Days
})
}));
}
posted @ 2010-08-01 11:00  熊哥  阅读(2210)  评论(4编辑  收藏  举报