How to set a property in Powershell on an instance of a class that implements IDictionary and ICollection

c#, powershell, sqlite

Solution

If you are trying to set this when you are creating the object it is possible using the `Property` parameter of `New-Object`:

$csb = New-Object -TypeName System.Data.SQLite.SQLiteConnectionStringBuilder -Property @{ DataSource = "C:\data\mydb.db" }

However if you are trying to set the property at some later time, the only way I have found is somewhat clumsy, using the .NET Reflection API:

$csb.GetType().GetProperty("DataSource").SetValue($csb, "C:\data\mydb.db")

Problem

I'm trying to work with the `SQLiteConnectionStringBuilder` class in the ADO.NET SQLite data provider library found here. The class definition has a property called DataSource. ``` public sealed class SQLiteConnectionStringBuilder : DbConnectionStringBuilder { public SQLiteConnectionStringBuilder(); public SQLiteConnectionStringBuilder(string connectionString); public string DataSource { get; set; } } ``` which I'm trying to set in my powershell script, viz: ``` $csb = new-object -TypeName System.Data.SQLite.SQLiteConnectionStringBuilder $csb.DataSource = "C:\data\mydb.db"; ``` The problem I'm having is that `SQLiteConnectionStringBuilder` inherits from `DbConnectionStringBuilder` which implements the following interfaces ``` public class DbConnectionStringBuilder : IDictionary, ICollection, IEnumerable, .... ``` Because the class implements these interfaces Powershell is NOT setting the property DataSource on the class but is treating `$csb` as a hash table and adding a key value pair to it i.e. `key="DataSource", value="C:\data\mydb.db"`. Needless to say this is not what I want. So what do I do now ? How to force Powershell set the property and not treat $csb to be a hash table? Thanks!

Original source