代码改变世界

Enum.Has 续:public static class GenericEnumExtensions

2011-01-06 02:34  Nana's Lich  阅读(577)  评论(1编辑  收藏  举报
.assembly GenericEnumExtensions
{
  .custom instance void [mscorlib]System.Runtime.Versioning.TargetFrameworkAttribute::.ctor(string) = ( 01 00 29 2E 4E 45 54 46 72 61 6D 65 77 6F 72 6B   // ..).NETFramework
                                                                                                        2C 56 65 72 73 69 6F 6E 3D 76 34 2E 30 2C 50 72   // ,Version=v4.0,Pr
                                                                                                        6F 66 69 6C 65 3D 43 6C 69 65 6E 74 01 00 54 0E   // ofile=Client..T.
                                                                                                        14 46 72 61 6D 65 77 6F 72 6B 44 69 73 70 6C 61   // .FrameworkDispla
                                                                                                        79 4E 61 6D 65 1F 2E 4E 45 54 20 46 72 61 6D 65   // yName..NET Frame
                                                                                                        77 6F 72 6B 20 34 20 43 6C 69 65 6E 74 20 50 72   // work 4 Client Pr
                                                                                                        6F 66 69 6C 65 )                                  // ofile

  .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 ) 
  //.custom instance void [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78   // ....T..WrapNonEx
  //                                                                                                           63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 )       // ceptionThrows.
  .custom instance void [System.Core]System.Runtime.CompilerServices.ExtensionAttribute::.ctor() = ( 01 00 00 00 ) 
  //.hash algorithm 0x00008004
  //.ver 0:0:0:0
}

.class public abstract auto ansi sealed beforefieldinit GenericEnumExtensions.GenericEnumExt
       extends [mscorlib]System.Object
{
  .custom instance void [System.Core]System.Runtime.CompilerServices.ExtensionAttribute::.ctor() = ( 01 00 00 00 ) 

	.method public hidebysig static bool  Has<valuetype .ctor ([mscorlib]System.Enum) T>(!!T target, !!T flag) cil managed
	{
		.custom instance void [System.Core]System.Runtime.CompilerServices.ExtensionAttribute::.ctor() = ( 01 00 00 00 ) 

		.maxstack  8
		ldarga     0
		ldarg.1
		box        !!T
		constrained. !!T
		callvirt   instance bool [mscorlib]System.Enum::HasFlag(class [mscorlib]System.Enum)
		ret
	}


	.method public hidebysig static valuetype [mscorlib]System.Nullable`1<!!T> 
					ParseEnum<valuetype .ctor ([mscorlib]System.Enum) T>(string target) cil managed
	{
		.custom instance void [System.Core]System.Runtime.CompilerServices.ExtensionAttribute::.ctor() = ( 01 00 00 00 ) 

		.maxstack  8
		ldarg.0
		ldc.i4.0
		call       valuetype [mscorlib]System.Nullable`1<!!T> GenericEnumExtensions.GenericEnumExt::ParseEnum<!!T>(string, bool)
		ret
	}

	.method public hidebysig static valuetype [mscorlib]System.Nullable`1<!!T> 
					ParseEnum<valuetype .ctor ([mscorlib]System.Enum) T>(string target, bool ignoreCase) cil managed
	{
		.custom instance void [System.Core]System.Runtime.CompilerServices.ExtensionAttribute::.ctor() = ( 01 00 00 00 ) 

		.maxstack  3
		.locals init (valuetype [mscorlib]System.Nullable`1<!!T>, valuetype [mscorlib]System.Nullable`1<!!T>)
		.try
		{
			ldtoken    !!T
			call       class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
			ldarg.0
			ldarg.1
			call       object [mscorlib]System.Enum::Parse(class [mscorlib]System.Type, string, bool)
			unbox.any  !!T
			newobj     instance void valuetype [mscorlib]System.Nullable`1<!!T>::.ctor(!0)
			stloc.0
			leave.s    L_Return
		}
		catch [mscorlib]System.Object 
		{
			pop
			ldloca     1
			initobj    valuetype [mscorlib]System.Nullable`1<!!T>
			ldloc.1
			stloc.0
			leave.s    L_Return
		}
		L_Return:  
		ldloc.0
		ret
	}

  //========//========//========//========
  //========//========//========//========

  .method public hidebysig static string 
          FormatEnum<valuetype .ctor ([mscorlib]System.Enum) T>(object target, string format) cil managed
  {
    .custom instance void [System.Core]System.Runtime.CompilerServices.ExtensionAttribute::.ctor() = ( 01 00 00 00 ) 

    .maxstack  8
    ldtoken    !!T
    call       class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
    ldarg.0
    ldarg.1
    call       string [mscorlib]System.Enum::Format(class [mscorlib]System.Type, object, string)
    ret
  }

  .method public hidebysig static string 
          Format<valuetype .ctor ([mscorlib]System.Enum) T>(!!T target, string format) cil managed
  {
    .custom instance void [System.Core]System.Runtime.CompilerServices.ExtensionAttribute::.ctor() = ( 01 00 00 00 ) 

    .maxstack  8
    ldtoken    !!T
    call       class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
    ldarg.0
    box        !!T
    ldarg.1
    call       string [mscorlib]System.Enum::Format(class [mscorlib]System.Type, object, string)
    ret
  }

  .method public hidebysig static string 
          GetEnumName<valuetype .ctor ([mscorlib]System.Enum) T>(object target) cil managed
  {
    .custom instance void [System.Core]System.Runtime.CompilerServices.ExtensionAttribute::.ctor() = ( 01 00 00 00 ) 

    .maxstack  8
    ldtoken    !!T
    call       class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
    ldarg.0
    call       string [mscorlib]System.Enum::GetName(class [mscorlib]System.Type, object)
    ret
  }

  .method public hidebysig static string[] 
          GetNames<valuetype .ctor ([mscorlib]System.Enum) T>() cil managed
  {
    .maxstack  8
    ldtoken    !!T
    call       class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
    call       string[] [mscorlib]System.Enum::GetNames(class [mscorlib]System.Type)
    ret
  }

  .method public hidebysig static class [mscorlib]System.Type 
          GetUnderlyingType<valuetype .ctor ([mscorlib]System.Enum) T>() cil managed
  {
    .maxstack  8
    ldtoken    !!T
    call       class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
    call       class [mscorlib]System.Type [mscorlib]System.Enum::GetUnderlyingType(class [mscorlib]System.Type)
    ret
  }

  .method public hidebysig static !!T[]  GetValues<valuetype .ctor ([mscorlib]System.Enum) T>() cil managed
  {
    .maxstack  8
    ldtoken    !!T
    call       class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
    call       class [mscorlib]System.Array [mscorlib]System.Enum::GetValues(class [mscorlib]System.Type)
    castclass  !!T[]
    ret
  }

  .method public hidebysig static bool  IsDefinedEnum<valuetype .ctor ([mscorlib]System.Enum) T>(object target) cil managed
  {
    .custom instance void [System.Core]System.Runtime.CompilerServices.ExtensionAttribute::.ctor() = ( 01 00 00 00 ) 

    .maxstack  8
    ldtoken    !!T
    call       class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
    ldarg.0
    call       bool [mscorlib]System.Enum::IsDefined(class [mscorlib]System.Type, object)
    ret
  }

}

 

// 程序集 GenericEnumExtensions.dll, v4.0.30319

using System;
using System.Runtime.CompilerServices;

namespace GenericEnumExtensions
{
  public static class GenericEnumExt
  {
    public static string Format<T>(this T target, string format) where T : struct, Enum;
    public static string FormatEnum<T>(this object target, string format) where T : struct, Enum;
    public static string GetEnumName<T>(this object target) where T : struct, Enum;
    public static string[] GetNames<T>() where T : struct, Enum;
    public static Type GetUnderlyingType<T>() where T : struct, Enum;
    public static T[] GetValues<T>() where T : struct, Enum;
    public static bool Has<T>(this T target, T flag) where T : struct, Enum;
    public static bool IsDefinedEnum<T>(this object target) where T : struct, Enum;
    public static T? ParseEnum<T>(this string target) where T : struct, Enum;
    public static T? ParseEnum<T>(this string target, bool ignoreCase) where T : struct, Enum;
  }
}