得到类型的所有公共属性名称

本文介绍了一个使用C#反射来获取指定类的所有公共属性名称的方法。通过实例演示了如何利用Reflection API遍历并获取一个名为Person类的所有属性名。

 

 

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

namespace John.Research.CSharp.AssemblyRes
{
    public class PropertiesReflector
    {

        public string GetPropertieNames()
        {
            Type myType=typeof(Person);
           
            StringBuilder sb = new StringBuilder();

            PropertyInfo[] fields = myType.GetProperties();
            foreach (PropertyInfo item in fields)
            {
                if (sb.Length == 0)
                {
                    sb.Append(item.Name);
                }
                else
                {
                    sb.Append("," + item.Name);
                }
              
            }
            return sb.ToString();
           


        }
    }

    public class Person
    {
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        private string title;

        public string Title
        {
            get { return title; }
            set { title = value; }
        }

        private string job;

        public string Job
        {
            get { return job; }
            set { job = value; }
        }

        public Person(string name, string title)
        {
            this.Name = name;
            this.Title = title;
        }
    }
}

### 如何在编程中检测指定类型的方法名称 为了实现对特定类型的实例所拥有的方法名进行检测,在不同编程语言中有不同的方式。以下是几种常见编程语言中的做法。 #### Python 中使用 `dir()` 函数获取对象属性列表并过滤出方法名 Python 提供了一个内置函数叫做 `dir()`,可以用来返回由字符串组成的列表,该列表包含了给定对象的所有有效属性的名字。对于类的实例来说,这其中包括了所有的数据成员以及继承自基类的数据成员和方法。如果想要专门找出其中的方法,则可以通过简单的逻辑判断来进行筛选: ```python def get_methods(obj_type): """Return a list of method names from the given type.""" attributes = dir(obj_type) methods = [ attr for attr in attributes if callable(getattr(obj_type, attr)) and not attr.startswith("__") ] return methods ``` 此段代码会遍历传入类型的全部属性,并通过 `callable` 来测试每一个属性是否是可以调用的对象(即方法),同时排除掉双下划线开头的特殊方法[^1]。 #### Java 使用反射 API 获取 Class 对象上的公共方法声明 Java 的反射机制允许运行期间动态地访问类的信息及其结构。利用 java.lang.reflect.Method 类可以从任意 class 文件里提取公开的方法签名信息。下面的例子展示了怎样取得 String 类里的所有 public 方法名字: ```java import java.lang.reflect.Method; public class Main { public static void main(String[] args) throws Exception { Method[] methods = String.class.getMethods(); for (Method m : methods){ System.out.println(m.getName()); } } } ``` 这段程序将会打印出 java.lang.String 这个类里面定义的所有公有级别的方法名称[^2]。 #### C++ 利用 RTTI 和标准库辅助工具查询类型信息 C++ 支持运行时类型识别(RTTI),这意味着可以在编译后的二进制文件内保留有关类型的一些元数据。然而值得注意的是,不像其他高级语言那样提供了直接枚举类成员的功能,因此通常需要借助第三方库如 Boost.TypeIndex 或者编写模板特化来完成这项工作。这里给出一个基于 typeid 关键字简单示例: ```cpp #include <iostream> #include <typeinfo> struct Base {}; struct Derived : public Base {}; int main(){ const std::type_info& ti = typeid(Base); std::cout << "Type name: " << ti.name() << '\n'; // Note that this only gives us information about the exact type, // Not its members or base classes. } ``` 上述例子仅能获得类型的标识符而非具体字段或方法详情;更深入的操作可能涉及复杂的宏定义或是依赖于外部框架的支持[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值