Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 7025

Deprecated: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 2162

Deprecated: Hook imagify_allow_picture_tags_for_webp is deprecated since version 2.2! Use imagify_allow_picture_tags_for_nextgen instead. in /www/collab365_296/public/wp-includes/functions.php on line 5758
How to call a generic method with Powershell and Reflection - Collab365
Deprecated: strstr(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1145

Deprecated: stripos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1152

Deprecated: stripos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1155

Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1162

Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1165

Deprecated: strstr(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1145

Deprecated: stripos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1152

Deprecated: stripos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1155

Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1162

Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1165

Warning: Undefined array key "url" in /www/collab365_296/public/wp-content/plugins/thrive-visual-editor/thrive-dashboard/inc/smart-site/classes/class-tvd-smart-shortcodes.php on line 85

2013-11-12

I am a developer by trade so am very used to using generics and reflection in C#, however, today I was faced with trying to grab an ‘SPPersistedObject’ from the ‘SPFarm.Local.Services.GetValue<T>()’ method.

So, in C# I wanted to do this :

Microsoft.Practices.SharePoint.Common.Logging.DiagnosticsService msDiagnosticService = SPFarm.Local.Services.GetValue<Microsoft.Practices.SharePoint.Common.Logging.DiagnosticsService>("clbDiagnosticService");
if (msDiagnosticService != null)
{
msDiagnosticService.Delete();
}

The above is removing a logger called ‘clbDiagnosticService’ if one exists. The problem line comes on the method ‘GetValue’ as that takes a generic type..

So, after a bit of “Googling” + trial and error .. the final Powershell looks like this :

and in code for those that want to copy and modify it :

# First load object models 
[System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.Practices.SharePoint.Common”)
[System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”)

$farm = [Microsoft.SharePoint.Administration.SPFarm]::Local

# Now we need to use Reflection to get the "GetValue" method..
$method = [Microsoft.SharePoint.Administration.SPServiceCollection].GetMethod("GetValue", [string])

Try
{
 $closedmethod =  $method.MakeGenericMethod[Microsoft.Practices.SharePoint.Common.Logging.DiagnosticsService]) 

 # get the service!
 $service = $closedmethod.Invoke($farm.Services,"clbDiagnosticService")

  # Do your stuff here...
 if ($service) 
 {
  Write-host $service.Id 
 }
}
Catch
{
  "Collaboris Logger Not Found - Stopping." 
}

Hope this helps!

 

(Visited 764 times, 1 visits today)

About the author 

Mark Jones

Collab365 Founder helping people learn Microsoft 365 via these:

👉 Collab365 Summits - Massive virtual conferences for Microsoft products
👉 Collab365 Today - Aggregation site for the best community blogs
👉 Collab365 Community - Huge blog site including plenty of Microsoft content

I want to provide a friendly online community, where we can learn and grow together:

👉 365ers - coming very soon!

Summit Bundle

Get 200+ hours of Microsoft 365 Training for 27$!

Master Office 365, Power Platform & SharePoint & Teams With 200+ Hours Of Training Videos and 108 Ebooks in the Collab365 Academy. This offer is insane and is only available for a limited period.