.net - 如何正确地将 C# 代码添加到 PowerShell 脚本?
问题描述
$w=@'
using System;
using System.Drawing;
public class img{
public static String test(){
Image image = Image.FromFile("C:\\img.png");
String size = image.Width+"x"+image.Height;
return size;
}
}
'@
add-type -typed $w
[img]::test()
我想添加这个 c# 代码,但我得到了错误。如何正确添加c#代码?
解决方案
由于 C# 代码使用 System.Drawing,因此必须知道Add-Type
它的来源。
Add-Type 有-ReferencedAssemblies参数可以做到这一点。像这样,
Add-Type -TypeDefinition $w -ReferencedAssemblies System.Drawing
推荐阅读
- amazon-web-services - 如何使用 CloudFormation 创建一个带有 DocumentType 包的“AWS::SSM::Document”
- angular - 如何将外部边框应用于有角材料的表格?
- ms-access - 在水晶报表中打印自定义记录
- javascript - 如何在雷达的 Chartjs 标签中增加尺寸和系列
- php - 邮政编码点火器
- c - 如何从示例中获取 LZW 编码结果?
- magento2 - 如何为每个商店获取一个 robots.txt
- jmeter - 在 jmeter、blazemeter 中生成会话 ID
- c++ - (Win32) 如何启动对话框?
- algorithm - 视频编辑中如何实现撤消/重做功能