C#Type類學習

C#Type類學習

using System;
using System.Reflection;
namespace 類型_Type
{
    /// <summary>
    /// Type Class Represents type declaration:class types,interface types,array types,array types,enumeration
    /// types,type parameters,generic type definitions,and open or closed constructed generic types
    /// 
    /// Inheritance: Object---→MemberInfo----→Type
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            // The code example uses the MethodInfo to invoke the substring method on the string "Hello World"
            // and display the result

            Type t = typeof(String);
            MethodInfo substr = t.GetMethod("Substring", new Type[] { typeof(int), typeof(int) });
            object result = substr.Invoke("Hello,World", new object[] { 3, 5 });
            Console.WriteLine($"{substr} returned {result}");

            Console.ReadKey();



        }
    }
}

Retrieving a type object

The type object associated wtih a particular type can be obtained in the following ways:

  • The instance of Object.GetType method returns a Type object that represents the type of an instance.Because all managed types derived from Object,and GetType method can be called an instance of any type.

The Following example calls Object.GetType method to determine the runtime of each object in an object array

   object[] values = { "hello", 'c', 1, 3.5, true };
            foreach(object item in values)
            {
                Console.WriteLine($"{item}---{item.GetType().Name}");
            }
// The example displays the following output:
// hello---String
// c---Char
// 1---Int32
// 3.5---Double
// True---Boolean

Comparing type objects for equality

A Type object that reperesents a type is unique.that is,two Type object references refer to the same object if and only if they represent the same object.This allows for comparison of Type objects using reference equality.The following example compares the Type objects that represent a number of intgeger values to determine whether they are same type.

     long number1 = 1635429;
            int number2 = 1000;
            double number3 = 3.14;
            List<object> values = new List<object>();
            values.Add(number3);
            values.Add(number1); values.Add(number2);
            foreach (var item1 in values)
            {
                foreach (var item2 in values)
                {
                    IsEquel(item1, item2);
                }
            }
            // The example displays the following output:
// 3.14 is System.Double, 3.14 is System.Double and their types are same
// 3.14 is System.Double, 1635429 is System.Int64 and their types are not same
// 3.14 is System.Double, 1000 is System.Int32 and their types are not same
// 1635429 is System.Int64, 3.14 is System.Double and their types are not same
// 1635429 is System.Int64, 1635429 is System.Int64 and their types are same
// 1635429 is System.Int64, 1000 is System.Int32 and their types are not same
// 1000 is System.Int32, 3.14 is System.Double and their types are not same
// 1000 is System.Int32, 1635429 is System.Int64 and their types are not same
// 1000 is System.Int32, 1000 is System.Int32 and their types are same

參考

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章