Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 250
john2lob Avatar asked Nov 30 '13 03:11

john2lob


People also ask

How do I set properties in PowerShell?

The Set-ItemProperty cmdlet changes the value of the property of the specified item. You can use the cmdlet to establish or change the properties of items. For example, you can use Set-ItemProperty to set the value of the IsReadOnly property of a file object to $True .

What does :: mean in PowerShell?

From the about_Operators help topic: :: Static member operator Calls the static properties operator and methods of a .NET Framework class. To find the static properties and methods of an object, use the Static parameter of the Get-Member cmdlet. [

What is @{} in PowerShell?

@{} in PowerShell defines a hashtable, a data structure for mapping unique keys to values (in other languages this data structure is called "dictionary" or "associative array"). @{} on its own defines an empty hashtable, that can then be filled with values, e.g. like this: $h = @{} $h['a'] = 'foo' $h['b'] = 'bar'

How do you declare a dictionary in PowerShell?

You can create an ordered dictionary by adding an object of the OrderedDictionary type, but the easiest way to create an ordered dictionary is use the [ordered] attribute. The [ordered] attribute is introduced in PowerShell 3.0. Place the attribute immediately before the "@" symbol.


1 Answers

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")
like image 162
Mike Zboray Avatar answered Sep 28 '22 06:09

Mike Zboray