PowerShell & C# :- Exposing properties

Background

Playing around with Powershell and arrived at an error that I will like to talk about a bit.

Error

Here is the error :-

Cannot set the Value property for PSMemberInfo object

of type “System.Management.Automation.PSMethod”.

Code

Let us recreate the error by sharing our code :-

Microsoft .Net Class – friend.cs


using System;
using System.Collections.Generic;
using System.Collections;

public class friend
{

    ArrayList objListofFriends = null;

    public friend()
    {
        objListofFriends = new ArrayList();
    }

    public void clearLists()
    {
        objListofFriends.Clear();
    }

    public void friendAdd(String friend)
    {
        objListofFriends.Add(friend);
    }

    public void friendAddRange(ArrayList objList)
    {
        objListofFriends.AddRange(objList);
    }

    // return list of Friend as a method
    public ArrayList listofFriendsAsMethod()
    {
        return (objListofFriends);

    }

    // return list of Friend as a property
    public ArrayList listofFriendsAsProperty
    {
        get
        {
            return (objListofFriends);
        }   

    }

}

Microsoft Powershell Class


Set-StrictMode -Version Latest

#Microsoft Powershell Module
[string] $file = "";
[string] $fileFullPath = "";
[string] $source = "";

[array] $objList = $null;
[string] $strLogFormat = "";
[string] $strLog = "";

[string] $errorMessage = "";
[string] $failedItem = "";

[int] $countofArrayList=0;

[object] $objProperty = $null;

$file = 'friend.cs';
$fileFullPath = resolve-path $file;
$source = [System.IO.File]::ReadAllText($file);

$strLog = "Import Files :- fileFullPath: {0}"-f $fileFullPath
Write-Debug $strLog
Add-Type -TypeDefinition $source -Language CSharpVersion3;

[friend] $objFriend =$null;

$objFriend = New-Object friend;

#To add friends
$objFriend.friendAdd("John Smith");
$objFriend.friendAdd("Sandra B.")

# to get list of friends
$objList = $objFriend.listofFriendsAsProperty;

#to get object type and number of entries
$strLogFormat = "Object Type {0}.  Using {1}, count is {2}";

try
{

    $countofArrayList = ($objFriend.listofFriendsAsMethod).Length;

    $strLog = $strLogFormat -f`
                      $objList.getType()`
                    , "length"`
                    , $countofArrayList`
                    ;

    Write-Host($strLog);

}
catch
{

    $errorMessage = $_.Exception.Message;

    $strLog = "Exception on {0}.  Exception is {1}" -f`
                    "($objFriend.listofFriendsAsMethod).Length"`
                  , $errorMessage`
                ;

    Write-Warning -Message $strLog;

}

try
{

    $countofArrayList = ($objFriend.listofFriendsAsMethod).Count;

    $strLog = $strLogFormat -f`
                      ($objFriend.listofFriends).getType()`
                    , "count"`
                    , $countofArrayList`
                    ;

    Write-Host($strLog);

}
catch
{

    $errorMessage = $_.Exception.Message;

    $strLog = "Exception on {0}.  Exception is {1}" -f`
                    "($objFriend.listofFriendsAsMethod).Count"`
                  , $errorMessage`
                ;

    Write-Warning -Message $strLog;

}

try
{

    $objProperty = $objFriend.listofFriendsAsProperty;

    $countofArrayList = $objProperty.Count;

    $strLog = $strLogFormat -f`
                      ($objList).getType()`
                    , "count"`
                    , $countofArrayList`
                    ;

    Write-Host($strLog);

}
catch
{

    $errorMessage = $_.Exception.Message;

    $strLog = "Exception on {0}.  Exception is {1}" -f`
                    "($objFriend.listofFriends).Count"`
                  , $errorMessage`
                ;

    Write-Warning -Message $strLog;

}

Output

output.20181229.1008AM.PNG

Explanation

  1. It is likely that one has to expose data as a property rather than just as a method

Errors

Other Errors

If in C# Source Code the Object’s data are not properly “exposed” as properties, you might possibly get errors such as

  1. Cannot set the Value property for PSMemberInfo object of type “System.Management.Automation.PSMethod”.

To get better data on how the Object’s data are exposed use the getType() method call.

 

Source Code Control

GitHub

DanielAdeniji/PowershellAndCSharpProperty
Link

 

References

  1. Microsoft
    • Docs
      • Module :- Microsoft.PowerShell.Utility

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s