Skip to content

Instantly share code, notes, and snippets.

@jagchat
Last active October 21, 2020 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jagchat/fbf2fd17ffd74d164becf57ac00d16e3 to your computer and use it in GitHub Desktop.
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).
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}");
}
}
}
namespace AnotherLib
{
public class Messages
{
public string GetMessage(string a)
{
var prefix = System.Configuration.ConfigurationManager.AppSettings["MsgPrefix"];
return $"{prefix}: {a}";
}
}
}
[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