`
eve
  • 浏览: 13171 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
最近访客 更多访客>>
社区版块
存档分类
最新评论

Extension methods

    博客分类:
  • c#
阅读更多
public static T As<T>(this object obj) where T : class
{
	return obj as T;
}

public static bool Contains(this string source, string toCheck, StringComparison comp)
{
	return source.IndexOf(toCheck, comp) >= 0;
}

public static bool Contains(this IEnumerable<string> container, string toCheck, StringComparison comp)
{
	return container.Any(c => string.Equals(c, toCheck, comp));
}
public static string GetDescription(this Enum value)
{
	var field = value.GetType().GetField(value.ToString());
	var attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)).As<DescriptionAttribute>();
	return attribute == null ? value.ToString() : attribute.Description;
}

public static T DeepClone<T>(this T obj)
{
	using (var ms = new MemoryStream())
	{
		var formatter = new BinaryFormatter();
		formatter.Serialize(ms, obj);
		ms.Position = 0;

		return (T)formatter.Deserialize(ms);
	}
}

public static T DeepCloneDataContract<T>(this T obj)
{
	using (var ms = new MemoryStream())
	{
		var dcs = new DataContractSerializer(typeof(T));

		dcs.WriteObject(ms, obj);
		ms.Position = 0;

		return (T)dcs.ReadObject(ms);
	}
}
public static object GetPropValue(this object src, string propName)
{
	try
	{
		return src.GetType().GetProperty(propName).GetValue(src, null);
	}
	catch
	{
		return string.Empty;
	}
}

public static string ToExcelColumn(this int columnIndex)
{
	var columnString = "";
	var columnNumber = columnIndex;
	while (columnNumber > 0)
	{
		var currentLetterNumber = (columnNumber - 1) % 26;
		var currentLetter = (char)(currentLetterNumber + 65);
		columnString = currentLetter + columnString;
		columnNumber = (columnNumber - (currentLetterNumber + 1)) / 26;
	}
	return columnString;
}

public static int ToColumnIndex(this string excelColumn)
{
	var retVal = 0;
	var col = excelColumn.ToUpper();
	for (var iChar = col.Length - 1; iChar >= 0; iChar--)
	{
		var colPiece = col[iChar];
		var colNum = colPiece - 64;
		retVal = retVal + colNum * (int)Math.Pow(26, col.Length - (iChar + 1));
	}
	return retVal;
}

 public static string GetVariableName<T>(this Expression<Func<T>> expr)
        {
            return expr.Body.As<MemberExpression>().Member.Name;
        }

 

分享到:
评论

相关推荐

    Z.ExtensionMethods, C# 扩展方法|. NET 开放源代码&免费库.zip

    Z.ExtensionMethods, C# 扩展方法|. NET 开放源代码&免费库 使用超过 1000种扩展方法,增强了. NET 架构。下载完整版本 NuGetZ.ExtensionMethods ( 推荐推荐) Z.ExtensionMethods.WithObjectNamespace

    How To Use Extension Methods_src

    How To Use Extension Methods_src

    C#例子代码 A0016_ExtensionMethods

    C#例子代码 A0016_ExtensionMethodsC#例子代码 A0016_ExtensionMethodsC#例子代码 A0016_ExtensionMethodsC#例子代码 A0016_ExtensionMethodsC#例子代码 A0016_ExtensionMethodsC#例子代码 A0016_ExtensionMethodsC#...

    ComparereTreino1:Treinando Expresses Lambda,Compareson,ExtensionMethods和功能

    ComparereTreino1:Treinando Expresses Lambda,Compareson,ExtensionMethods和功能

    CSharpExtensionMethods:我的C#扩展方法

    C#扩展方法我的集合和POCO对象的C#扩展方法。 享受! :collision:您可以通过安装所有它们 :hammer:或最小包装。或必备包(首选)。目录:集合扩展方法(命名空间-Pylypeiev.Extensions):POCO扩展方法(名称空间-...

    数据库的相关操作

    数据库的增删改查

    MVC3 Video Tutorial One

    MVC 3 – Razor View Engine By Jon Galloway|April 4, 2011 In this video you will learn the...In this video, you will learn how to use both extension methods and declarative @helper syntax in Razor views.

    AW - Essential C# 4.0, 3rd Edition Mar 2010+完美版

    Methods and parameters–including extension methods, partial meth&shy;ods, and C# 4.0’s optional and named parameters Generics, concurrent collections, and custom collections with iterators ...

    LINQ for Dummies

    •Examine .NET language extensions and work with extension methods, partial methods, lambda expressions, and query expressions •LINQ to DataSet operators, SQL server operations, XML API, or Active ...

    ASP.net 权限系统

    Lm.Common.ExtensionMethods 一些使编码更加方便的扩展方法 Lm.Common.Security.Cryptography 加密 Lm.Common.Threading 同类任务线型、不同类任务并行 Lm.Common.Web web开发中比较便捷的代码 Lm.Common.Web....

    通用数据访问层DAL Excel 操作 权限管理 128b 条码生成

    Lm.Common.ExtensionMethods 一些使编码更加方便的扩展方法 Lm.Common.Security.Cryptography 加密 Lm.Common.Threading 同类任务线型、不同类任务并行 Lm.Common.Web web开发中比较便捷的代码 Lm.Common.Web....

    C#用户权限控制架构,可以控制到类

    Lm.Common.ExtensionMethods 一些使编码更加方便的扩展方法 Lm.Common.Security.Cryptography 加密 Lm.Common.Threading 同类任务线型、不同类任务并行 Lm.Common.Web web开发中比较便捷的代码 Lm.Common...

    Head First C# 英文版 7-6

    this book covers Visual C# 2008, Visual Studio 2008, and the .NET Framework 3.5, and teaches everything from language fundamentals to advanced topics including garbage collection, extension methods, ...

    Head First C# 英文版 7-7

    this book covers Visual C# 2008, Visual Studio 2008, and the .NET Framework 3.5, and teaches everything from language fundamentals to advanced topics including garbage collection, extension methods, ...

    Head First C# 英文版 7-3

    this book covers Visual C# 2008, Visual Studio 2008, and the .NET Framework 3.5, and teaches everything from language fundamentals to advanced topics including garbage collection, extension methods, ...

    Head First C# 英文版 7-1

    this book covers Visual C# 2008, Visual Studio 2008, and the .NET Framework 3.5, and teaches everything from language fundamentals to advanced topics including garbage collection, extension methods, ...

    Head First C# 英文版 7-5

    this book covers Visual C# 2008, Visual Studio 2008, and the .NET Framework 3.5, and teaches everything from language fundamentals to advanced topics including garbage collection, extension methods, ...

    Head First C# 英文版 7-4

    this book covers Visual C# 2008, Visual Studio 2008, and the .NET Framework 3.5, and teaches everything from language fundamentals to advanced topics including garbage collection, extension methods, ...

    Head First C# 英文版 7-2

    this book covers Visual C# 2008, Visual Studio 2008, and the .NET Framework 3.5, and teaches everything from language fundamentals to advanced topics including garbage collection, extension methods, ...

Global site tag (gtag.js) - Google Analytics