How to reference .NET assemblies using PowerShell

后端 未结 2 723
[愿得一人]
[愿得一人] 2020-11-27 16:56

I am a C# .NET developer/architect and understand that it uses objects (.NET objects) and not just streams/text.

I would like to be able to use PowerShell to call me

2条回答
  •  天涯浪人
    2020-11-27 17:17

    Take a look at the blog post Load a Custom DLL from PowerShell:

    Take, for example, a simple math library. It has a static Sum method, and an instance Product method:

    namespace MyMathLib
    {
        public class Methods
        {
            public Methods()
            {
            }
    
            public static int Sum(int a, int b)
            {
                return a + b;
            }
    
            public int Product(int a, int b)
            {
                return a * b;
            }
        }
    }
    

    Compile and run in PowerShell:

    > [Reflection.Assembly]::LoadFile("c:\temp\MyMathLib.dll")
    > [MyMathLib.Methods]::Sum(10, 2)
    
    > $mathInstance = new-object MyMathLib.Methods
    > $mathInstance.Product(10, 2)
    

提交回复
热议问题