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

Extension methods

    博客分类:
  • c#
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

    描述中的信息虽简洁,但我们可以推断这可能是一个源代码文件夹,名为"ExtensionMethods",其中包含了各种示例和练习,以展示如何在实际项目中应用扩展方法。 标签 "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和功能

    在C#编程中,"ComparereTreino1"项目似乎是一个练习或教程,专注于训练开发者在实际编程中使用的关键技术,包括Lambda表达式、比较器(Compareson)、扩展方法(Extension Methods)以及函数式编程概念。让我们逐一...

    扩展Unity3d 组件方法,简化API使用 - C#特性之 Extension Method

    在Unity3D中,Extension Methods可以用来简化对 GameObject、Transform、Collider 等常用组件的访问。例如,创建一个扩展方法来快速设置游戏物体的位置: ```csharp public static class GameObjectExtensions { ...

    CSharpExtensionMethods:我的C#扩展方法

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

    数据库的相关操作

    数据库是存储和管理数据的核心工具,它在信息技术领域扮演着至关重要的角色。"数据库的相关操作"主要涵盖了四个基本概念:增加(Add)、删除(Delete)、修改(Modify)和查询(Query),也就是我们常说的CRUD操作。...

    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.

    Xtend User Guide

    **知识点5:注解(Annotations)和扩展方法(Extension Methods)** Xtend提供了强大的注解支持,允许开发者在代码中使用注解来提供额外的信息和控制,以及通过扩展方法增强现有类的功能。这些扩展方法可以来自于库...

    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的增删改查源码.rar

    在C#中,LINQ与extension methods和expression trees紧密关联,提供了更简洁、更易于理解和维护的代码。 1. **LINQ的查询表达式**: LINQ查询表达式是一种声明式编程模式,它允许开发者用类似于SQL的语法在C#中...

    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....

    Functional Programming

    Extension methods in C# allow us to add methods to existing types without creating a new derived type or recompiling the original class. They are implemented as static methods in a static class, but ...

    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, ...

Global site tag (gtag.js) - Google Analytics