如何使用C#中的Reflections从.cs文件中获取类和方法? [英] How to get Classes and methods from a .cs file using Reflections in C#.?

查看:49
本文介绍了如何使用C#中的Reflections从.cs文件中获取类和方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取".cs"文件中可用的类?就像我们可以使用以下方法在Assembly中获取类和方法一样,

How to get the classes that are available in a '.cs' file.? Like we can get the classes and methods in an Assembly using,

Assembly.GetTypes() and Type.GetMethods()

获取程序集中的类和方法.

to get the Class and methods in an Assembly.

类似地,如何获取C#文件(.cs文件)中存在的所有类.我需要在.cs文件中获取,从中我可以轻松地获取其中的方法以及方法参数等更多详细信息.

Similarly how to get all the classes present within a C# file(.cs file).? I need to get the classes in a .cs file from which i can easily get the methods within them and further details like parameters of methods etc.

推荐答案

使用C#解析器的时间短,没有直接的方法.您可以使用 CSharpCodeProvider 编译 .cs 文件(仅在文件自行编译并且可以将所有引用的程序集告知编译器的情况下才有效),并在生成的程序集.

Short of using a C# parser, there's no direct way of doing it. You could compile the .cs file using CSharpCodeProvider (which only works if the file compiles on its own and you can tell all the referenced assemblies to the compiler) and use reflection on the resulting assembly.

这篇关于如何使用C#中的Reflections从.cs文件中获取类和方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆