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

参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值