StreamReader的不能引用程序读取的代码的FileStream

问题描述 投票:0回答:1

我需要分析性质在与方法的类里面正由我的一个NuGet包主动的解决方案引用自定义的框架。我可以读取文件上我积极的解决方案成功,因为我可以使用该文件的本地路径。我也可以拉从引用架组件文件的FileStream,但StreamReader的只写有“MZ”,并且由于文件是从NuGet包,我没有本地路径。

这里有三种解决方法我试过,我在评论中运行的问题:

//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个回答
1
投票

当你建立一个.NET程序中不包含源代码;它被编译成IL(在密集二进制形式,而不是作为文本IL源),以及IL被发送。你可以在本地做这个事实是纯粹为你的机器上的源文件的事故。这不是一般的应用。

基本上,你会需要不同的选项。 .NET有一个完整的反射API,允许你检查在运行时非常多的元数据(包括字段,方法,属性,属性等) - 但它不包括实际的源代码,这是因为:你不(通常)船舶这一点。

如果你能具体谈谈你需要目前fileString是什么做什么,我们或许可以指导你的最佳方法,什么是/是不可能的,等等。

© www.soinside.com 2019 - 2024. All rights reserved.