【发布时间】: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