PowerTip of the Day-Creating Your Own Types
原文地址:http://app.en25.com/e/es.aspx?s=1403&e=5072&elq=a8251b41fe1a4d6297a0aecd8e466373
原文:
Did you know that you can compile any .NET source code on the fly and use this to create your own types? Here is an example illustrating how to create a new type from c# code that has both static and dynamic methods:
$source = @'
public class Calculator
{
public static int Add(int a, int b)
{
return (a + b);
}
public int Multiply(int a, int b)
{
return (a * b);
}
}
'@
Add-Type -TypeDefinition $source
[Calculator]
[Calculator]::Add(5,10)
$myCalculator = New-Object Calculator
$myCalculator.Multiply(3,12)
翻译:
你知道你可以编译任何即时.net源代码并且作为你的自定义类型使用它们吗?以下是一个示例阐述如何通过c#来创建一个拥有静态方法和实例方法的类型:
$source = @'
public class Calculator
{
public static int Add(int a, int b)
{
return (a + b);
}
public int Multiply(int a, int b)
{
return (a * b);
}
}
'@
Add-Type -TypeDefinition $source
[Calculator]
[Calculator]::Add(5,10)
$myCalculator = New-Object Calculator
$myCalculator.Multiply(3,12)
笔记:
直接在ps代码中创建一个类。
复习.net类的静态方法和实例方法的调用方法。
个人倾向于用先把逻辑编译成dll然后在ps里调用。
---------------------------------------------------------------
aspnetx的BI笔记系列索引:
使用SQL Server Analysis Services数据挖掘的关联规则实现商品推荐功能
---------------------------------------------------------------
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步