【问题标题】:StreamReader cannot read FileStream of code from referenced assemblyStreamReader 无法从引用的程序集中读取代码的 FileStream
【发布时间】:2019-02-06 00:39:34
【问题描述】:

我需要分析自定义框架中的类内部的属性和方法,我的活动解决方案正在使用 nuget 包引用该框架。 我可以成功读取活动解决方案中的文件,因为我可以使用文件的本地路径。 我也可以从引用的框架程序集中提取文件的 FileStream,但 StreamReader 只读取“MZ�”,并且由于文件来自 Nuget 包,因此我没有本地路径。

以下是我尝试过的三个解决方案以及我在 cmets 中遇到的问题:

//attempt at using FileStream
                PortableExecutableReference location =
                    MetadataReference.CreateFromFile(Assembly.Load(assemblyName).Location);
                var assembly = Assembly.LoadFrom(location.FilePath);
                FileStream f = assembly.GetFiles()
                    .Where(t => type.Name == name).FirstOrDefault();
                if (f != null)
                {
                    using (StreamReader sr = new StreamReader(f))
                    {
                        while (!sr.EndOfStream)
                        {
                            fileString = sr.ReadLine();
                        }
                    }
                }
                //StreamReader does not read the FileStream, fileString value = "MZ�"

//attempt at geting the path thru f.Name
                PortableExecutableReference location =
                    MetadataReference.CreateFromFile(Assembly.Load(assemblyName).Location);
                var assembly = Assembly.LoadFrom(location.FilePath);
                FileStream f = assembly.GetFiles()
                    .Where(t => type.Name == name).FirstOrDefault();
                return f.Name;
                //only returns the framework path. StreamReader can't read the file with this and since its metadata I can't find the file in framework solution.

//attempt to at least get the base class information
                Assembly a = Assembly.GetAssembly(typeof(type));
                using (FileStream fs = a.GetFile(name))
                {
                    using (StreamReader sr = new StreamReader(fs))
                    {
                        return fileString = sr.ReadLine();
                    }
                }
                //fs = null

fileString 应该以字符串的形式包含我的类文件。

【问题讨论】:

    标签: c# .net nuget filestream streamreader


    【解决方案1】:

    在构建 .NET 程序时不包含源代码;它被编译为 IL(以密集的二进制形式,而不是作为文本 IL 源),并发送 IL。您可以在本地执行此操作的事实纯粹是您的机器上有源文件的意外。这一般不适用。

    基本上,您将需要一个不同的选项。 .NET 有一个完整的反射 API,允许您在运行时检查大量元数据(包括字段、方法、属性、属性等) - 但它包含实际的源代码,因为: 你(通常)不发货。

    如果您可以更具体地使用当前fileString 的内容,我们可能会指导您采用最佳方法,以及哪些是/不可能的,等等.

    【讨论】:

    • 我正在使用 Roslyn 语法树来解析类并提取方法和属性,然后将值显示为表单中的字符串。这是我用来解析活动解决方案中类的方法的方法: SyntaxTree tree = CSharpSyntaxTree.ParseText(flowString); var syntaxRoot = (CompilationUnitSyntax)tree.GetRoot(); var method = syntaxRoot.DescendantNodes().OfType() .Where(md => md.Identifier.ValueText.Equals("string")) .FirstOrDefault();
    • @Kabber 你在名字/签名之后吗?还是真实的身体?名字和签名可以通过反射获得——实际的身体......不是那么多。您可以考虑将源嵌入为资源,但是... Eesh
    • @Marc Gravell 我现在关注名称和方法体,最终我将需要属性名称和值。
    • @Kabber 属性名称和值可通过反射获得,方法名称也是如此;方法体不是——你可以得到 IL,但这并不能返回原始源代码,即使你通过反编译器运行它
    猜你喜欢
    • 2012-07-16
    • 2012-12-14
    • 1970-01-01
    • 1970-01-01
    • 2013-02-10
    • 1970-01-01
    • 2010-09-22
    • 1970-01-01
    • 2011-05-05
    相关资源
    最近更新 更多