Csharp/C Sharp/Data Types/Type
Версия от 15:31, 26 мая 2010; (обсуждение)
Содержание
Check the MinValue and MaxValue
using System;
class MinAndMax {
public static void Main() {
Console.WriteLine("sbyte: {0} to {1}", sbyte.MinValue,sbyte.MaxValue);
Console.WriteLine("byte: {0} to {1}", byte.MinValue,byte.MaxValue);
Console.WriteLine("short: {0} to {1}", short.MinValue,short.MaxValue);
Console.WriteLine("ushort: {0} to {1}", ushort.MinValue,ushort.MaxValue);
Console.WriteLine("int: {0} to {1}", int.MinValue,int.MaxValue);
Console.WriteLine("uint: {0} to {1}", uint.MinValue,uint.MaxValue);
Console.WriteLine("long: {0} to {1}", long.MinValue,long.MaxValue);
Console.WriteLine("ulong: {0} to {1}", ulong.MinValue,ulong.MaxValue);
Console.WriteLine("float: {0} to {1}", float.MinValue,float.MaxValue);
Console.WriteLine("double: {0} to {1}", double.MinValue,double.MaxValue);
Console.WriteLine("decimal: {0} to {1}", decimal.MinValue,decimal.MaxValue);
}
}
Examining a Currently Running Process for Type Information
using System;
using System.Reflection;
using System.Diagnostics;
class AssemType {
public static void Main(string[] args) {
Process p = Process.GetCurrentProcess();
string assemblyName = p.ProcessName + ".exe";
Console.WriteLine("Examining : {0}", assemblyName);
Assembly a = Assembly.LoadFrom(assemblyName);
Type[] types = a.GetTypes();
foreach(Type t in types)
{
Console.WriteLine("\nType : {0}",t.FullName);
Console.WriteLine("\tBase class: {0}",t.BaseType.FullName);
}
}
}
Querying Type Information by Name
using System;
using System.Reflection;
class NameType
{
public static void Main()
{
Type t = Type.GetType("System.String");
Console.WriteLine("Name : {0}",t.Name);
Console.WriteLine("Underlying System Type : {0}",t.UnderlyingSystemType);
Console.WriteLine("Is Class : {0}",t.IsClass);
}
}
Querying Type Information Using the Instance of an Object
using System;
using System.Reflection;
class InstanceType
{
public static void Main()
{
String myVar = "a string";
Type t = myVar.GetType();
Console.WriteLine("Name : {0}",t.Name);
Console.WriteLine("Underlying System Type: {0}",t.UnderlyingSystemType);
Console.WriteLine("Is Class : {0}",t.IsClass);
}
}