csharp http://stackoverflow.com/questions/5915740/how-to-convert-a-typecode-to-an-actual-type/357029
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp http://stackoverflow.com/questions/5915740/how-to-convert-a-typecode-to-an-actual-type/357029相关的知识,希望对你有一定的参考价值。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace SwitchVsDictionary
{
static class Program
{
static void Main(string[] args)
{
var typeCodes = TypeCodeToTypeMap.Keys.ToList();
int iterations = 10000000;
Benchmark(() =>
{
foreach(var typeCode in typeCodes)
{
typeCode.ToTypeDict();
}
}, iterations);
Benchmark(() =>
{
foreach (var typeCode in typeCodes)
{
typeCode.ToTypeSwitch();
}
}, iterations);
}
/// <summary>
/// Table that maps TypeCode to it's corresponding Type.
/// </summary>
static IReadOnlyDictionary<TypeCode, Type> TypeCodeToTypeMap = new Dictionary<TypeCode, Type>
{
{ TypeCode.Boolean, typeof(bool) },
{ TypeCode.Byte, typeof(byte) },
{ TypeCode.Char, typeof(char) },
{ TypeCode.DateTime, typeof(DateTime) },
{ TypeCode.DBNull, typeof(DBNull) },
{ TypeCode.Decimal, typeof(decimal) },
{ TypeCode.Double, typeof(double) },
{ TypeCode.Empty, null },
{ TypeCode.Int16, typeof(short) },
{ TypeCode.Int32, typeof(int) },
{ TypeCode.Int64, typeof(long) },
{ TypeCode.Object, typeof(object) },
{ TypeCode.SByte, typeof(sbyte) },
{ TypeCode.Single, typeof(Single) },
{ TypeCode.String, typeof(string) },
{ TypeCode.UInt16, typeof(UInt16) },
{ TypeCode.UInt32, typeof(UInt32) },
{ TypeCode.UInt64, typeof(UInt64) }
};
/// <summary>
/// Convert a TypeCode ordinal into it's corresponding Type instance.
/// </summary>
public static Type ToTypeDict(this TypeCode code)
{
return TypeCodeToTypeMap[code];
}
public static Type ToTypeSwitch(this TypeCode code)
{
switch (code)
{
case TypeCode.Boolean:
return typeof(bool);
case TypeCode.Byte:
return typeof(byte);
case TypeCode.Char:
return typeof(char);
case TypeCode.DateTime:
return typeof(DateTime);
case TypeCode.DBNull:
return typeof(DBNull);
case TypeCode.Decimal:
return typeof(decimal);
case TypeCode.Double:
return typeof(double);
case TypeCode.Empty:
return null;
case TypeCode.Int16:
return typeof(short);
case TypeCode.Int32:
return typeof(int);
case TypeCode.Int64:
return typeof(long);
case TypeCode.Object:
return typeof(object);
case TypeCode.SByte:
return typeof(sbyte);
case TypeCode.Single:
return typeof(Single);
case TypeCode.String:
return typeof(string);
case TypeCode.UInt16:
return typeof(UInt16);
case TypeCode.UInt32:
return typeof(UInt32);
case TypeCode.UInt64:
return typeof(UInt64);
}
return null;
}
/// <summary>
/// From http://stackoverflow.com/questions/1622440/benchmarking-method-calls-in-c-sharp
/// </summary>
private static void Benchmark(Action act, int iterations)
{
GC.Collect();
Stopwatch sw = Stopwatch.StartNew();
for (int i = 0; i < iterations; i++)
{
act.Invoke();
}
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
}
}
}
以上是关于csharp http://stackoverflow.com/questions/5915740/how-to-convert-a-typecode-to-an-actual-type/357029的主要内容,如果未能解决你的问题,请参考以下文章
csharp 实例-CSHARP-GroupDocs.Conversion.Examples.CSharp变频-ConvertWithCustomInputDataHandler.cs
csharp 例如-CSHARP-GroupDocs.Conversion.Examples.CSharp - 程序 - ConvertFilesToDifferentFormats.cs
csharp 例如-CSHARP-GroupDocs.Conversion.Examples.CSharp渲染,RenderPSDImageAsPath.cs
csharp 实例-CSHARP-GroupDocs.Conversion.Examples.CSharp变频-ConvertTopresentationAsPath.cs
csharp 实例-CSHARP-GroupDocs.Conversion.Examples.CSharp变频-ConvertTopresentationAsPath.cs
csharp 实例-CSHARP-GroupDocs.Conversion.Examples.CSharp变频-ConvertTopresentationAsPath.cs