CSharp examples for System:Type
Cast Type
using System.Threading.Tasks; using System.Text; using System.Runtime.InteropServices; using System.Reflection; using System.Linq.Expressions; using System.Linq; using System.Collections.Generic; using System;//from w w w .j a v a 2s .c o m using SafeILGenerator.Ast.Nodes; public class Main{ static public object CastType(object Value, Type CastType) { if (CastType == typeof(sbyte)) return (sbyte)Convert.ToInt64(Value); if (CastType == typeof(short)) return (short)Convert.ToInt64(Value); if (CastType == typeof(int)) return (int)Convert.ToInt64(Value); if (CastType == typeof(long)) return (long)Convert.ToInt64(Value); if (CastType == typeof(byte)) return (byte)Convert.ToInt64(Value); if (CastType == typeof(ushort)) return (ushort)Convert.ToInt64(Value); if (CastType == typeof(uint)) return (uint)Convert.ToInt64(Value); if (CastType == typeof(ulong)) return (ulong)Convert.ToInt64(Value); if (CastType == typeof(float)) return Convert.ToSingle(Value); if (CastType == typeof(double)) return Convert.ToDouble(Value); if (CastType.IsEnum) return Enum.ToObject(CastType, Value); throw(new NotImplementedException("CastType for type '" + CastType + "'")); } static public TType CastType<TType>(object Value) { return (TType)CastType(Value, typeof(TType)); } }