Created
June 3, 2014 21:20
-
-
Save Fhernd/3dc1734b5ae40f645bf9 to your computer and use it in GitHub Desktop.
Demostración del método GetType de Object en C#.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Text; | |
namespace Recetas.Cap03 | |
{ | |
internal class UsoGetType | |
{ | |
public static void Main() | |
{ | |
// Uso de typeof para obtener la información del | |
// tipo `StringBuilder`: | |
Type tipo1 = typeof(StringBuilder); | |
Console.WriteLine("\nNombre de clase completamente calificado para `StringBuilder`: {0}", | |
tipo1.AssemblyQualifiedName); | |
// Obtención de información del tipo `System.String` con | |
// el método estático `Type.GetType`: | |
Type tipo2 = Type.GetType("System.String"); | |
Console.WriteLine("\nNombre de clase completamente calificado para `System.String`: {0}", | |
tipo2.AssemblyQualifiedName); | |
// Con el true especificamos una búsqueda sensible | |
// a caracteres: | |
Type tipo3 = Type.GetType("System.String", true); | |
Console.WriteLine("\nNombre de clase completamente calificado para `System.String`: {0}", | |
tipo3.AssemblyQualifiedName); | |
// Con `GetTpe(String, Boolean, Boolean)` especificamos si queremos que | |
// se lanza una excepción cuando se encuentre el tipo y consideramos | |
// entre mayúsculas y minúsculas | |
Type tipo4 = Type.GetType("system.string", true, true); | |
Console.WriteLine("\nNombre de clase completamente calificado para `System.String`: {0}", | |
tipo4.AssemblyQualifiedName); | |
// Tipo de un nombre completamente calificado: | |
Type tipo5 = Type.GetType(String.Format("{0}{1}{2}", | |
"System.Data.DataSet,System.Data,", | |
"Version=2.0.0.0,Culture=neutral,", | |
"PUblicKeyToken=b77a5c561934e089")); | |
Console.WriteLine("\nNombre de clase completamente calificado para `System.Data.DataSet`: {0}", | |
tipo5.AssemblyQualifiedName); | |
// Uso del método `Object.GetType`: | |
StringBuilder sb = new StringBuilder(); | |
Type tipo6 = sb.GetType(); | |
Console.WriteLine("\nNombre de clase completamente calificado para `StringBuilder`: {0}\n", | |
tipo6.AssemblyQualifiedName); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment