用PowerShell创建C#dll和use方法 [英] Create C# dll and use method with powershell

查看:404
本文介绍了用PowerShell创建C#dll和use方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个简单的C#DLL,并使用 RegAsm.exe 进行注册。一个非常简单的方法被调用没有参数并返回一个数字,这里是一个简化的版本。

I created a simple C# dll and registered it using RegAsm.exe. One very simple method is invoked without parameters and returns a number, here is a simplified version.

namespace AVL_test {
    interface ITestClass {
        int GetNumber();
    }

    [ComVisible(true)]
    public class TestClass: ITestClass {
        public TestClass() { }

        public int GetNumber() {
            return 10;
        }
    }
}

我需要调用该方法从Powershell,所以我添加了类型

I need to invoke that method from Powershell, so I added the type

Add-Type -Path "C:\myPath\AVL_test.dll"

似乎加载,因为如果我 [AVL_test.TestClass] 我得到这个输出

It seems to be loaded because if I [AVL_test.TestClass] I get this output


IsPublic IsSerial Name BaseType

-------- - ------- ---- --------

True False TestClass System.Object

IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True False TestClass System.Object

但是如果我尝试通过键入 [AVL_test.TestClass] :: GetNumber() GetNumber() c>我得到这个错误

But if I try to invoke GetNumber() by typing[AVL_test.TestClass]::GetNumber() I get this error


方法调用失败,因为[AVL_test.TestClass]不包含名为GetNumber的方法。 >

Method invocation failed because [AVL_test.TestClass] does not contain a method named 'GetNumber'.

我做错了什么?

推荐答案

您的方法应为 static ,或者您需要创建一个该类型的实例(TestClass)。

Your method should be static or you need to create an instance of that type (TestClass).

最后可以使用

New-Object -TypeName <full qualified type name> -ArgumentList <args>

或在您的具体情况下:

$test = New-Object -TypeName AVL_Test.TestClass
$test.GetNumber()

这篇关于用PowerShell创建C#dll和use方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆