C# 反射手记

本文通过具体的代码示例介绍了如何使用C#中的反射API来获取类的各种信息,包括成员、字段、属性及方法等,并展示了如何调用静态方法和获取其实现细节。

从代码中学习吧

可以把 TestInterface 块里的方法放进 Main 执行来对应的理解

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

namespace ReflectionLearn
{
    class Program
    {
        static void Main(string[] args)
        {
            PrintClassStaticMethodInfo();
        }

        #region TestInterface

        #region GetMembers
        private static void PrintClassPublicInfo(Type varType)
        {
            //返回公开成员;
            MemberInfo[] tempMemInfos = varType.GetMembers();
            ShowMemberConsoleInfo(tempMemInfos);
        }
        private static void PrintClassAllInfo(Type varType)
        {
            //包含非公开、包含实例成员、包含公开;
            //此处含父类的成员;
            MemberInfo[] tempMemInfos = varType.GetMembers(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
            ShowMemberConsoleInfo(tempMemInfos);
        }
        private static void PrintClassSelfInfo(Type varType)
        {
            //包含非公开、包含实例成员、包含公开;
            //BindingFlags.DeclaredOnly 表明 不含父类的成员;
            MemberInfo[] tempMemInfos = varType.GetMembers(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
            ShowMemberConsoleInfo(tempMemInfos);
        }

        private static void PrintClassSelfInclueStaticInfo(Type varType)
        {
            //包含非公开、包含实例成员、包含公开;
            //BindingFlags.DeclaredOnly 表明 不含父类的成员;
            MemberInfo[] tempMemInfos = varType.GetMembers(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Static);
            ShowMemberConsoleInfo(tempMemInfos);
        }
        #endregion

        #region GetFields

        private static void PrintClassFieldsInfo()
        {
            Type tempType = typeof(RefClass);
            RefClass tempRc = new RefClass();
            tempRc.pIntMember = 3;

            FieldInfo[] tempFieInfos = tempType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Static);
            foreach (FieldInfo tempInfo in tempFieInfos)
            {
                if (tempInfo.Name == "mIntMember")
                {
                    tempInfo.SetValue(tempRc,999);
                }
                if (tempInfo.Name == "<mIntProperty>k__BackingField")
                {
                    //属性方法;
                    tempInfo.SetValue(tempRc, 777);
                }
                Console.WriteLine("名称: " + tempInfo.Name + "  --  类型: " + GetMemberType(tempInfo.MemberType) + " - 值: " + tempInfo.GetValue(tempRc));
            }
            Console.ReadKey();
        }

        private static void PrintClassPropertyInfo()
        {
            Type tempType = typeof(RefClass);
            RefClass tempRc = new RefClass();
            tempRc.pIntMember = 3;

            PropertyInfo[] tempPropertyInfos = tempType.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Static);
            foreach (PropertyInfo tempInfo in tempPropertyInfos)
            {
                MethodInfo tempGetInfo = tempInfo.GetGetMethod(true);
                Console.WriteLine("get方法的名称{0}  返回值类型:{1}  参数数量:{2}  MSIL代码长度:{3} 局部变量数量:{4}", 
                    tempGetInfo.Name, tempGetInfo.ReturnType.ToString(),
                    tempGetInfo.GetParameters().Count(),
                    tempGetInfo.GetMethodBody().GetILAsByteArray().Length, 
                    tempGetInfo.GetMethodBody().LocalVariables.Count);

                MethodInfo tempSetInfo = tempInfo.GetSetMethod(true);
                Console.WriteLine("get方法的名称{0}  返回值类型:{1}  参数数量:{2}  MSIL代码长度:{3} 局部变量数量:{4}", 
                    tempSetInfo.Name, tempSetInfo.ReturnType.ToString(),
                    tempSetInfo.GetParameters().Count(),
                    tempSetInfo.GetMethodBody().GetILAsByteArray().Length,
                    tempSetInfo.GetMethodBody().LocalVariables.Count);

               tempSetInfo.Invoke(tempRc, new object[] { 666 });
               object obj = tempGetInfo.Invoke(tempRc, null);
               Console.WriteLine("方法名:{0}  内部值:{1}", tempInfo.Name, obj);
            }
            Console.ReadKey();
        }

        #endregion

        #region StaticMethods
        private static void PrintClassStaticMethodInfo()
        {
            Type tempType = typeof(RefClass);
            RefClass tempRc = new RefClass();
            tempRc.pIntMember = 3;

            MethodInfo[] tempPropertyInfos = tempType.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Static);
            foreach (MethodInfo tempInfo in tempPropertyInfos)
            {
                if (tempInfo.GetParameters().Count() > 0 && tempInfo.GetParameters()[0].ParameterType == typeof(string) )
                {
                    object obj = tempInfo.Invoke(tempRc, new[] { "123" });
                    MethodBody mbody = tempInfo.GetMethodBody();
                    Console.WriteLine("拥有参数的方法名:{0}  返回值类型:{1}  参数1类型:{2}  参数1名称:{3}  方法调用后返回的值:{4}",
                        tempInfo.Name,
                        tempInfo.ReturnType.ToString(),
                        tempInfo.GetParameters()[0].ParameterType.ToString(),
                        tempInfo.GetParameters()[0].Name,
                        obj.ToString());
                }
                else
                {
                    MethodBody mbody = tempInfo.GetMethodBody();
                    Console.WriteLine("没有参数的方法名:{0}  返回值类型:{1}",
                        tempInfo.Name,
                        tempInfo.ReturnType.ToString());
               }
            }
            Console.ReadKey();
        }
        #endregion

        #endregion

        #region LogicBusiness
        private static void ShowMemberConsoleInfo(MemberInfo[] varMemInfos)
        {
            foreach (MemberInfo tempInfo in varMemInfos)
            {
                Console.WriteLine("名称: " + tempInfo.Name + "  --  类型: " + GetMemberType(tempInfo.MemberType));
            }
            Console.ReadKey();
        }


        private static string GetMemberType(MemberTypes varMemType)
        {
            string tempTypeStr = string.Empty;
            switch (varMemType)
            {
                case MemberTypes.Field:
                    {
                        tempTypeStr = "字段";
                    }break;
                case MemberTypes.Method:
                    {
                        tempTypeStr = "方法";
                    }break;
                case MemberTypes.Property:
                    {
                        tempTypeStr = "属性";
                    }break;
                case MemberTypes.Constructor:
                    {
                        tempTypeStr = "构造函数";
                    } break;
                case MemberTypes.Custom:
                    {
                        tempTypeStr = "自定义成员";
                    } break;
                case MemberTypes.Event:
                    {
                        tempTypeStr = "事件";
                    } break;
                case MemberTypes.NestedType:
                    {
                        tempTypeStr = "嵌套";
                    } break;
                case MemberTypes.TypeInfo:
                    {
                        tempTypeStr = "类型";
                    } break;
                default:
                    {
                        tempTypeStr = "未知";
                    }break;
            }
            return tempTypeStr;
        }

        #endregion

    }

    #region DefineInfo

    public class RefClass
    {
        private int mIntMember;
        private int mIntProperty { get; set; }
        protected int mProteInt;
        public int pIntMember { get; set; }

        public CustomClassOne mCustomClass;

        public void AnyOne()
        {

        }
        public static void ThisIsStaticMethodOne()
        {

        }
        public static void ThisIsStaticMethodTwo()
        {

        }

        public static string HadReturnValueA(string varStr)
        {
            int tempIntA;
            int tempIntB;
            return varStr;
        }
        public static string HadReturnValueB(string varStr)
        {
            string tempStr;
            return varStr;
        }
    }

    public class CustomClass
    {
        public int MCustomInt;

    }

    public class CustomClassOne : CustomClass
    {
        public int MCustomInt;
        public static CustomClassTwo mCustomClassTwo;
    }

    public class CustomClassTwo : CustomClass
    {
        public int MCustomInt;

    }

    #endregion

}
<think>好的,用户想要了解如何从PPT或者有很多图片的PDF中提取文字。我需要先理清楚用户的需求,可能的场景,以及潜在的问题。首先,PPT图片型PDF中的文字通常是嵌入在图像中的,普通的复制粘贴方法不管用,必须用OCR(光学字符识别)技术。用户可能是个学生、教师,或者办公人员,需要处理课件、报告或者文档,想要提取里面的文字进行编辑或整理。 接下来,我需要考虑不同的解决方案,覆盖免费和付费工具,本地和在线服务,以及编程方法。常见的方法包括使用Adobe Acrobat这样的专业软件,或者免费的在线OCR网站。另外,手机应用也可能是一个方便的选择,特别是用微信QQ的扫描功能。对于技术背景的用户,可能需要提到Python库如PyMuPDF和Tesseract,或者开源工具如OCRmyPDF。 还要注意用户可能关心的问题,比如准确性、处理速度、隐私安全,尤其是处理敏感内容时在线工具的风险。此外,批量处理和大文件的支持也是需要考虑的点。需要提醒用户,如果排版复杂或图片质量差,OCR效果可能不佳,建议预处理图片。 然后,我应该分步骤介绍各种方案,从最简单易用的开始,逐步到更专业的工具。确保结构清晰,用户可以根据自己的需求选择合适的方法。最后总结关键点,帮助用户做出决策。检查是否有遗漏的工具或方法,确保信息准确,比如确认最新版的OneDrive和Google Drive是否支持OCR,以及常用软件是否有更新。</think>提取PPT图片型PDF中的文字,核心需依赖OCR(光学字符识别)技术。以下是分场景的解决方案: --- ### 一、**免费方案** 1. **Office自带功能** - **Word 2013+**:直接将PPT/PDF拖入Word,右键图片选「复制图片中的文本」 - **OneDrive网页版**:上传文件后右键选择「在Word中打开」,可提取图片文字 2. **微信/QQ快捷提取** - 手机截图后通过微信「扫一扫」或QQ「提取图中文字」功能识别 3. **在线OCR工具** - **iLovePDF** (https://www.ilovepdf.com/ocr-pdf):支持多语言,免费版有限额 - **Google Drive**:上传PDF→右键「用Google文档打开」自动OCR识别 --- ### 二、**专业软件方案** 1. **Adobe Acrobat Pro**(高精度首选) - 操作路径:工具→扫描和OCR→「识别文本」→选择输出格式 - 优势:支持批量处理、保留排版、多语言混合识别 2. **ABBYY FineReader**(学术场景推荐) - 特点:复杂表格/公式识别能力强,支持导出Markdown/Excel格式 --- ### 三、**编程实现(开发者适用)** ```python # 示例:Python + PyMuPDF + Tesseract import fitz # PyMuPDF import pytesseract from PIL import Image doc = fitz.open("input.pdf") for page in doc: pix = page.get_pixmap(dpi=200) # 设置分辨率 img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples) text = pytesseract.image_to_string(img, lang='chi_sim+eng') # 中英混合 print(text) ``` **依赖库安装**: ```bash pip install PyMuPDF pytesseract # 需额外安装Tesseract本体:https://github.com/UB-Mannheim/tesseract/wiki ``` --- ### 四、**开源工具推荐** - **OCRmyPDF** (命令行工具) ```bash ocrmypdf --output-type pdf --language chi_sim input.pdf output.pdf ``` - **Umi-OCR** (Windows GUI):支持离线批量处理(GitHub开源项目) --- ### **注意事项** 1. **精度提升技巧**: - 扫描文件建议预处理:PS/画图工具调整对比度>300dpi - 复杂排版优先用Adobe/ABBYY 2. **隐私保护**:敏感内容建议使用离线工具(如Umi-OCR) 3. **特殊内容**:手写体识别推荐「腾讯云OCR」或「百度文字识别」API 根据文件复杂度、预算和隐私需求选择方案,常规办公推荐组合:微信快速识别+Adobe处理专业文档。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值