[英]Using to get PS script name from a C# DLL? Getting "Object reference not set to an instance of an object."
ERROR:错误:
Object reference not set to an instance of an object.
Object 引用未设置为 object 的实例。
I'm trying to create a DLL that can get the Entrypoint application name that is written in Powershell.我正在尝试创建一个 DLL,它可以获取在 Powershell 中写入的 Entrypoint 应用程序名称。
The layout is very simple:布局非常简单:
In Powershell I add the line在 Powershell 中,我添加了这一行
Add-Type -Path ('C:\Users\Public\PS\DLL\get_value.dll')
then I create the object然后我创建 object
$value = New-Object PS_get_value.Stack
and call the C# method并调用 C# 方法
$rtn = $value.Get_stack()
If the Get_stack()
method returns a basic string like "Test"
it works fine….如果
Get_stack()
方法返回一个基本字符串,如"Test"
,它工作正常...... BUT if I try to use the .net Assembly.但是如果我尝试使用 .net 程序集。
GetEntryAssembly().Location
(which should return the full path to the calling (parent) app), I get the error above. GetEntryAssembly().Location
(应该返回调用(父)应用程序的完整路径),我得到上面的错误。
I am trying to get the full path (or at least the filename) of the application script that is running in Powershell. I plan on using that value to validate that the program (PS script) is the one that should be running.我正在尝试获取在 Powershell 中运行的应用程序脚本的完整路径(或至少是文件名)。我计划使用该值来验证该程序(PS 脚本)是否应该运行。 If there is a different way for the C# code to get it that's fine.
如果 C# 代码有不同的获取方式,那很好。 BTW if I run a normal program it works.
顺便说一句,如果我运行一个正常的程序,它就可以工作。 I do know that there is something about managed code base, I'm not sure what that is.
我确实知道有一些关于托管代码库的东西,但我不确定那是什么。
Thanks for any help or direction your can give.感谢您提供的任何帮助或指导。
Code Example:代码示例:
PS:附言:
Add-Type -Path ('C:\Users\Public\PS\DLL\get_value.dll')
$addNum = New-Object PS_get_value.Stack
$_rtnKey = $addNum.Get_stack()
Write-Host "RTNed Key Version 2: $_rtnKey"
C# (the DLL): C#(DLL):
using System;
using System.IO;
using System.Diagnostics;
using System.Reflection;
namespace PS_get_value
{
public class Stack{
public string Get_stack()
{
string _test_a = g_stack();
return _test_a;
}
public string g_stack()
{
return System.Reflection.Assembly.GetEntryAssembly().Location;
}
}
}
Give it a try by returning one of the following:通过返回以下之一来尝试一下:
or要么
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.