Posted By: mstefan | Feb 11th, 2007 @ 4:16 AM
page 1 of 1
Comments: 5 | Views: 4884
PowerShell has the ability to load an assembly and create an instance of a class; is there a way for the assembly to know that it has been loaded in PS rather than, for example, as a reference in a Windows Forms based application?

[Too quick clicking the wrong button there...]

How about this:

Assembly.GetEntryAssembly().GetName().Name == "PowerShell";

Is that what you're looking for?

Yggdrasil wrote:
How about this:

Assembly.GetEntryAssembly().GetName().Name == "PowerShell";

Is that what you're looking for?



Unfortunately that won't work. GetEntryAssembly returns null, presumably because the assembly is being loaded from unmanaged code.

It looks like I'm going to have to use an interop hack and call GetModuleFileName, but I'd prefer to not have to do that.
mstefan wrote:

Yggdrasil wrote: How about this:

Assembly.GetEntryAssembly().GetName().Name == "PowerShell";

Is that what you're looking for?



Unfortunately that won't work. GetEntryAssembly returns null, presumably because the assembly is being loaded from unmanaged code.

It looks like I'm going to have to use an interop hack and call GetModuleFileName, but I'd prefer to not have to do that.


Process.GetCurrentProcess().MainModule.FileName?

The same basic difference there. My issue isn't so much with interop as having to use "magic strings" like the module filename to determine what loaded the assembly. It looks like the solution is to get the module name and version info and do a bunch of sanity checks.

I guess what I'm really driving at is I'd like some way to reliably ask "Am I being loaded in a script, not a compiled executable?" so that certain decisions could be made about behavior, default property values and licensing.
page 1 of 1
Comments: 5 | Views: 4884