Typeof()与 GetType() ,获取对象的所有公有属性和所有公有方法GetProperties()GetMethods()

<1>

TypeOf()与GetType()的区别

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace @typeof
{
    // TypeOf() 和 GetType()的区别:  
    //<1> TypeOf():得到一个Class的Type
    //<2> GetType():得到一个Class的实例的Type



    //Typeof()是运算符而GetType是方法
    //GetType()是基类System.Object的方法,因此只有建立一个实例之后才能够被调用(初始化以后)
    //Typeof()的参数只能是int,string,String,自定义类型,且不能是实例
    class Program
    {        
        static void Main(string[] args)
        {
           //得到一个Class的Type
           var type = typeof(int);
           Console.WriteLine(type);  //输出System.int32




           //得到一个Class的Type
           var type3 = typeof(Program);
           Console.WriteLine(type3); //输出typeof.Program
           
           
            //-----------------------------------------------------------

           

           Program p = new Program();

           //得到一个Class的实例的Type
           var type2= p.GetType();
           Console.WriteLine(type2); //输出typeof.Program

           Console.ReadKey();
        }
    }
}


<.2>

获取对象类型的所有公有属性。GetProperties()  和所有公有方法GetMethods()

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace StringBuilders
{
    class User
    {
        public User()
        {

        }
        public void GetData()
        {

        }
        public int ID { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
  
        private string Gender { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            User u = new User();
            //获取u对象类型的所有公有属性。 即:获取User类的所有公有属性  它的返回值是PropertyInfo类型的数组
            PropertyInfo[] strObject = u.GetType().GetProperties();

            //获取User类的所以公有属性 (与上面那句代码效果一致)
            var strClass = typeof(User).GetProperties();



            foreach (PropertyInfo v in strObject) //遍历User类的所有公有属性
            {
                Console.WriteLine(v.Name);
            }


            Type c = typeof(User);
            //获取Type类型的所以公有方法。返回值是一个MethodInfo类型的数组
            MethodInfo[] methods = c.GetMethods();


            foreach (MethodInfo m in methods) //遍历User类的所有公有方法
            {
                Console.WriteLine(m.Name);
            }



            Console.ReadKey();
        }
    }
}


<3>

获取一个类实例的类型 GetType()

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    public class User
    {
        public string UserName { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            User u = new User(); //创建一个User类的对象 u

            Type t = u.GetType(); //获取 这个u对象的类型

            Console.WriteLine(t); //输出ConsoleApplication1.User
            Console.ReadKey();  
        }
    }
}



除了使用反射机制以外,TypeScript 还提供了其他一些方法获取类的所有属性方法。下面是两种常用的方法: 1. 使用 `Object.keys()` 函数来获取类的所有属性名,然后再通过属性获取对应的值。 ```typescript class MyClass { private privateProperty: string; public publicProperty: number; constructor() { this.privateProperty = 'private property'; this.publicProperty = 123; } private privateMethod() { console.log('private method'); } public publicMethod() { console.log('public method'); } } const myInstance = new MyClass(); // 获取类的所有属性 const properties = Object.keys(myInstance); console.log(properties); // ["privateProperty", "publicProperty"] // 获取类的所有方法 const methods = Object.keys(MyClass.prototype).filter((property) => typeof myInstance[property] === 'function'); console.log(methods); // ["privateMethod", "publicMethod"] ``` 2. 使用 `Reflect.ownKeys()` 函数来获取类的所有属性方法名称。 ```typescript class MyClass { private privateProperty: string; public publicProperty: number; constructor() { this.privateProperty = 'private property'; this.publicProperty = 123; } private privateMethod() { console.log('private method'); } public publicMethod() { console.log('public method'); } } const myInstance = new MyClass(); // 获取类的所有属性方法 const propertiesAndMethods = Reflect.ownKeys(myInstance); console.log(propertiesAndMethods); // ["privateProperty", "publicProperty", "privateMethod", "publicMethod"] ``` 这两种方法都可以获取类的所有属性方法,你可以根据自己的需求选择其中一种。需要注意的是,这些方法只能获取到实例属性方法,无法获取静态属性方法。如果你需要获取静态成员,你可以通过类本身直接访问。例如,`MyClass.staticProperty` 和 `MyClass.staticMethod`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值