Powershell custom type issue assessing passed object.
I am working with Powershell 5.1. I have created a new custom type which will take a psobject as an object parameter to the AddLog method. I want to change the index value on the passed object to update it with the current _counter value.
I have tried the following to no avail.
- //value["Index"] = _counter;
- //value.GetType().GetField("Index"); //.SetValue(value, _counter);
- //value.Index = _counter;
Can you tell me how I would change the passed objects Index field during the AddLog method call?
Thanks in advance.
Here is the custom type.
Add-Type @" using System; using System.ComponentModel; using System.Threading; using System.Runtime.CompilerServices;
public class MyClassObject : System.Collections.Concurrent.ConcurrentDictionary<int, object>, INotifyPropertyChanged{ public event PropertyChangedEventHandler PropertyChanged; public int _counter = 0;
public void OnPropertyChanged([CallerMemberName]string caller = null){
var handler = PropertyChanged;
if(handler != null){
handler(this, new PropertyChangedEventArgs(caller));
}
}
public Boolean AddLog(object value) {
Interlocked.Increment(ref _counter);
//value["Index"] = _counter;
//value.GetType().GetField("Index"); //.SetValue(value, _counter);
//value.Index = _counter;
var i = base.TryAdd(_counter, value);
OnPropertyChanged();
return i;
}
public MyClassObject() {
}
}
"@