Click here to Skip to main content
15,065,328 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted

1 solution

Short answer; No.

Slightly longer answer;
It would be somewhat difficult (as I am sure you're already suspecting), the reason for it being hard is that the value returned by the Location getter isn't stored in a backing field such as
C#
private readonly string location;


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);

C#
[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
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900