ISE: Modify Class

rated by 0 users
This post has 1 Reply | 2 Followers

Not Ranked
Posts 1
MontyX Posted: 10-31-2011 2:57 PM

I'm a newbie here, so Hello Everybody,

I would like to ask the following:

I can use ISE only for developing. Company policy.

I'm developing a script which contains a new object type, defined like that:

$DefClass = @'
public class PropType{
    public string name1;
    public string name2;
}
'@

Add-Type $DefClass

But every time, if I would like to modify a member, like the type of the property from string to Int, in that case I have to restart the whole PowerShell ISE which is importing the SnapIns too, and this is wasting my time very well.

Do you have any idea, how can I modify a property, or remove a property from a class. or just redefine a class?

Many thanks

 

Top 10 Contributor
Posts 260
Microsoft MVP
Top Contributor

If I understand you, you are wanting to re-define the class (PropType in your sample). Sadly that's not possible. Once a type is loaded into an Appdomain, which is what PowerShell runs in, you can're remove it. This is a feature. Two observations.

To some degree, you should be defining the classes you want to play with early on and avoid changing them. I guess that's obvious. So to some degree, this is a short term probelm at worst.

But second, in ISE< you can very easiy open a new PowerShell Tabl which creates a new PowerShell runspace into which you can load an updated version of the type. On my machine, a new PowerShell tab comes up, modules loaded, all 4 profiles files run in under 1.5 seconds.

The pattern then of how you use this is to do the definition of the type in one PowerShell tab. Edit it there. WHen you have a new version of yoru type, then open up a new PowerShell tab and install it. Then carry on your script debugging  in the new PowerShell tab. And you could event write a script (and attach it to the menu) to open all the relevant files in the new PowerShell tab to speed things up even more.

 

Thomas

Page 1 of 1 (2 items) | RSS
Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.