Last active
October 21, 2020 14:02
-
-
Save jagchat/fbf2fd17ffd74d164becf57ac00d16e3 to your computer and use it in GitHub Desktop.
- call a method in c# assembly (from powershell) which further refers to its own dependent assemblies
- the dependent assembly reads some info from app.config (to display msg).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace SampleDotNetFullLib | |
{ | |
public class Math | |
{ | |
public string GetMessage() | |
{ | |
var o = new AnotherLib.Messages(); | |
return o.GetMessage("in Math.GetMessage"); | |
} | |
public string GetSum(int a, int b) | |
{ | |
var o = new AnotherLib.Messages(); | |
return o.GetMessage($"Sum = {a + b}"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace AnotherLib | |
{ | |
public class Messages | |
{ | |
public string GetMessage(string a) | |
{ | |
var prefix = System.Configuration.ConfigurationManager.AppSettings["MsgPrefix"]; | |
return $"{prefix}: {a}"; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", $null) | |
Add-Type -AssemblyName System.Configuration | |
$config_path = "C:\temp\Powershell-DotNet-Samples\03\app.config" | |
[System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", $config_path) | |
[Configuration.ConfigurationManager].GetField("s_initState", "NonPublic, Static").SetValue($null, 0) | |
[Configuration.ConfigurationManager].GetField("s_configSystem", "NonPublic, Static").SetValue($null, $null) | |
([Configuration.ConfigurationManager].Assembly.GetTypes() | where { $_.FullName -eq "System.Configuration.ClientConfigPaths" })[0].GetField("s_current", "NonPublic, Static").SetValue($null, $null) | |
[System.AppDomain]::CurrentDomain.GetData("APP_CONFIG_FILE") | |
[System.Configuration.ConfigurationManager]::AppSettings | |
#main assembly | |
[Reflection.Assembly]::LoadFrom("C:\temp\Powershell-DotNet-Samples\03\SampleDotNetFullLib\SampleDotNetFullLib\bin\Debug\SampleDotNetFullLib.dll") | |
$oMath = new-object -TypeName "SampleDotNetFullLib.Math" | |
Write-Output $oMath.GetMessage() | |
Write-Output $oMath.GetSum(10, 20) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment