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

本文介绍了一种使用C#扩展方法来简化数据处理任务的技术,包括将对象转换为JSON字符串、选择对象属性、判断元素是否存在、判断元素是否在指定范围内等常见操作。通过这些扩展方法,开发者可以更高效地处理数据,提高代码的可读性和可维护性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
})
}));
}

转载于:https://www.cnblogs.com/livexy/archive/2010/08/01/1772605.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值