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

}

}

"@

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,978 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,576 questions
{count} votes

Your answer

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