Powershell custom type issue assessing passed object.

Boyd, Clifford 0 Reputation points
2024-11-22T19:27:19.9266667+00:00

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() {

}

}

"@

Windows for business | Windows Server | User experience | PowerShell
Developer technologies | .NET | Other
{count} votes

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.