从PowerShell脚本调用DLL的方法

Posted fits

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从PowerShell脚本调用DLL的方法相关的知识,希望对你有一定的参考价值。

$PSVersionTable.PSVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      18362  145     

 

创建一个Class,这个类包含了构造方法,静态方法和普通方法

using System;

namespace Cosmos.StudentForFrameWork
{
    public class Students
    {
        public string Name { get;  set; }
        public int Age { get; private set; }
        public Students(string name, int age)
        {
            Name = name;
            Age = age;
        }

        public static string StaticStudent(string name)
        {
            return "Hello:" + name;
        }

        public String GetInfo(string message)
        {
            return message + Name + Age;
        }

        public static string GetName
        {
            get { return "Do u want get my Name!"; }
        }
      
    }
}

 

把他编译成dll.

导入你的dll所在的地方

调用 dll 的构造方法 使用 ::new()
调用dll 的静态方法使用::MethodName()
Import-Module "$PSScriptRootCosmos.StudentForFrameWork.dll";  
$Stuent=[Cosmos.StudentForFrameWork.Students]::new($name,$age); 
$StudentInfo=$Stuent.GetInfo();
$StaticStu=[Cosmos.StudentForFrameWork.Students]::StaticStudent($name);


 

  

以上是关于从PowerShell脚本调用DLL的方法的主要内容,如果未能解决你的问题,请参考以下文章