本文介绍了如何修改.NET程序集只读属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用程序中在运行时修改Assembly.Location属性。我正在从资源加载.NET程序集,加载的程序集(从内存加载)将此字段清空,它会破坏应用程序功能。



我有无法控制其源代码。我知道如何在我自己的代码中解决这个bug,但事实并非如此。当他们使用Assembly.Location并获得一个空字符串时,问题就开始了。



[]



这是只读的。有没有低级别的方法呢?任何想法?



我的应用程序是嵌入在资源中的那些应用程序的加载器,因此它不依赖于它们。尝试从内存中加载任何程序集并检查那些已加载程序集的Assembly.Location字段,它将为空。他们没有位置,因为他们是从内存中加载的,我仍然希望通过.NET内部修改或任何其他方法来改变它。



压缩的程序集无法解压缩到磁盘。如果你只是知道如何实现它,我不介意重大问题的危险。

I want to modify Assembly.Location property at runtime in my application. I'm loading .NET Assemblies from the resources, and the loaded Assemblies (loaded from the memory) has this field empty and it breaks down application functionality.

I have no control over their source code. I know how to work around this bug in my own code, but that's not the case. And when they use Assembly.Location and gets an empty string the problem starts.

http://msdn.microsoft.com/en-us/library/system.reflection.assembly.location.aspx[^]

It's read only. Is there any low level way to do it? Any ideas?

My application is a loader for those applications embedded in the resources, so it doesn't depend on them. Try to load any assembly from the memory and check the Assembly.Location field of those loaded assemblies, it will be blank. They don't have the location because they're loaded from the memory, still I want to change that, either by .NET internals modifications or any other method.

The compressed assemblies cannot be decompressed to the disk. I don't mind the danger of major problems if you just know how to achieve it.

推荐答案

private readonly string location;





相反,它是使用程序集(或实际上,因为该类是抽象的, RuntimeAssembly 这是从内存加载内容时获得的子类)然后使用外部dll调用来读取位置。



它看起来像这样(简化);





Instead, it is fetched using the native handle to the Assembly (or actually since that class is abstract, RuntimeAssembly which is the sub-class you get when loading stuff from memory) and then using an external dll call to read the location.

It looks something like this (simplified);

[DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
private static extern void GetLocation(RuntimeAssembly assembly, StringHandleOnStack retString);

public override String Location {
  get {
    String location = null;
    GetLocation(GetNativeHandle(), JitHelpers.GetStringHandleOnStack(ref location));

    return location;
  }
}





我认为您更容易更改需要此代码的代码希望这有帮助,

弗雷德里克



I think it is going to be easier for you to change the code that requires this to be non-empty than inject a value into that.

Hope this helps,
Fredrik


这篇关于如何修改.NET程序集只读属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 21:25