C# 如何从已从字节加载的程序集获取文件版本?

C# 如何从已从字节加载的程序集获取文件版本?,c#,.net-4.0,assemblies,C#,.net 4.0,Assemblies,我有一些程序集存储在数据库中的Oracle BLOB字段中。我正在加载程序集,创建类的实例,等等,所有这些都是成功的。但是,我想访问已加载程序集的AssemblyFileVersion,但似乎无法找到如何执行该操作 我尝试了很多方法,包括下面的代码: var assembly = Assembly.Load(plugInBytes); var version = FileVersionInfo.GetVersionInfo(assembly.Location).FileVersion; 但是,

我有一些程序集存储在数据库中的Oracle BLOB字段中。我正在加载程序集,创建类的实例,等等,所有这些都是成功的。但是,我想访问已加载程序集的AssemblyFileVersion,但似乎无法找到如何执行该操作

我尝试了很多方法,包括下面的代码:

var assembly = Assembly.Load(plugInBytes);
var version = FileVersionInfo.GetVersionInfo(assembly.Location).FileVersion;
但是,当从字节加载程序集时,
assembly.Location
为空,之后不会有任何好的结果

只需在正确的方向上进行微调。

如果已应用,您不能只使用:

var version = assembly.GetCustomAttributes(typeof(AssemblyFileVersionAttribute), false)
                      .Cast<AssemblyFileVersionAttribute>()
                      .Select(attr => attr.Version)
                      .FirstOrDefault();
if (version != null)
{
    // Got the version number...
}
var version=assembly.GetCustomAttributes(typeof(AssemblyFileVersionAttribute),false)
.Cast()
.Select(attr=>attr.Version)
.FirstOrDefault();
如果(版本!=null)
{
//得到了版本号。。。
}
如果已应用,您不能只使用:

var version = assembly.GetCustomAttributes(typeof(AssemblyFileVersionAttribute), false)
                      .Cast<AssemblyFileVersionAttribute>()
                      .Select(attr => attr.Version)
                      .FirstOrDefault();
if (version != null)
{
    // Got the version number...
}
var version=assembly.GetCustomAttributes(typeof(AssemblyFileVersionAttribute),false)
.Cast()
.Select(attr=>attr.Version)
.FirstOrDefault();
如果(版本!=null)
{
//得到了版本号。。。
}
你可以试试看

 public bool GetVersion(string fileName)
 {
       Assembly asm = null;
       try
       {
               asm = Assembly.LoadFrom(fileName);
        }
        catch (Exception err)
        {
               this._errMsg = err.Message;
               return false;
         }
         if (asm != null)
         {
               this._info = new AssemblyInformation();
               this._info.Name = asm.GetName().Name;
               this._info.Version = asm.GetName().Version.ToString();
              this._info.FullName = asm.GetName().ToString();
         }
         else
         {
               this._errMsg = "Invalid assembly";
               return false;
          } 
          return GetReferenceAssembly(asm);
  }
  public bool GetVersion(Assembly asm)
  {
         if (asm != null)
         {
              this._info = new AssemblyInformation();
              this._info.Name = asm.GetName().Name;
             this._info.Version = asm.GetName().Version.ToString();
             this._info.FullName = asm.GetName().ToString();
         }
         else
          {
             this._errMsg = "Invalid assembly";
             return false;
          }

          return GetReferenceAssembly(asm);
    }
你可以试试看

 public bool GetVersion(string fileName)
 {
       Assembly asm = null;
       try
       {
               asm = Assembly.LoadFrom(fileName);
        }
        catch (Exception err)
        {
               this._errMsg = err.Message;
               return false;
         }
         if (asm != null)
         {
               this._info = new AssemblyInformation();
               this._info.Name = asm.GetName().Name;
               this._info.Version = asm.GetName().Version.ToString();
              this._info.FullName = asm.GetName().ToString();
         }
         else
         {
               this._errMsg = "Invalid assembly";
               return false;
          } 
          return GetReferenceAssembly(asm);
  }
  public bool GetVersion(Assembly asm)
  {
         if (asm != null)
         {
              this._info = new AssemblyInformation();
              this._info.Name = asm.GetName().Name;
             this._info.Version = asm.GetName().Version.ToString();
             this._info.FullName = asm.GetName().ToString();
         }
         else
          {
             this._errMsg = "Invalid assembly";
             return false;
          }

          return GetReferenceAssembly(asm);
    }

只需获取相同的字节,保存到临时文件,如果需要文件版本,则获取文件版本。汇编版本可能是相同的,并且更容易获得(见Jon的回复)

只需获取相同的字节,保存到临时文件,如果需要文件版本,则获取文件版本。汇编版本可能是相同的,并且更容易获得(见Jon的回复)

当您知道位置时,为什么不从位置加载它?@SaeedAmiri,位置在Oracle BLOB字段中。当您知道位置时,为什么不从位置加载它?@SaeedAmiri,位置在Oracle BLOB字段中field@AlexeiLevnekov,为了安全起见,我无法将程序集保存到磁盘reasons@AlexeiLevnekov,出于安全原因,我无法将程序集保存到磁盘