首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在-path和-language csharpversion3中使用add-type?

如何在-path和-language csharpversion3中使用add-type?
EN

Stack Overflow用户
提问于 2011-03-29 23:36:33
回答 2查看 6.2K关注 0票数 3

我一直在Powershell中使用add-type来动态编译一些我想使用的C#类。除了只有2.0版本之外,它工作得很好。

我刚刚发现了-language csharpversion3选项,但它不适用于-path。我该如何解决这个问题呢?

编辑:删除了关于ReadAllText的部分-我弄错了。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-03-31 00:51:34

以下是解决方法:将文件作为文本读取。

代码语言:javascript
复制
$script = [io.file]::readalltext($scriptpath)
add-type $script -lang csharpversion3

我不妨把剩下的部分粘贴进去,让这个答案在某种程度上有用..我有一个调试标志,可以让我生成DLL,这样我就可以更容易地使用调试器,使用Reflector检查它,等等。

代码语言:javascript
复制
$cp = new-object codedom.compiler.compilerparameters
$cp.ReferencedAssemblies.Add('system.dll') > $null
$cp.ReferencedAssemblies.Add('system.core.dll') > $null

# optionally turn on debugging support
if ($debugscript)
{
    # delete old unused crap while we're at it
    dir "$($env:temp)\-*.dll" |%{
        del $_ -ea silentlycontinue
        if ($?) { del $_.fullname.replace('.dll', '.pdb') -ea silentlycontinue }
    }

    $cp.TreatWarningsAsErrors = $true
    $cp.IncludeDebugInformation = $true
    $cp.OutputAssembly = $env:temp + '\-' + [diagnostics.process]::getcurrentprocess().id + '.dll'
}

$script = [io.file]::readalltext($scriptpath)
add-type $script -lang csharpversion3 -compilerparam $cp

如果$debugscript设置为true,这将添加一些额外的功能:

使用绑定到进程ID的特定DLL/PDB名称(在文件夹中),以便每个会话都有自己的名称。编译时出现警告errors

  • Generate a PDB
  • (在临时文件夹中)。这对于迭代对.cs的更改很有用。
  • 会删除旧的dll和pdb,这在迭代时也很好。
票数 5
EN

Stack Overflow用户

发布于 2011-03-30 01:31:21

我不知道它是否是你所需要的,但是你可以在powershell中启用.NET 4支持。请记住,默认情况下它使用XML2。为此,您需要在$PSHome中添加一个名称为powershell.exe.config和以下内容的.NET:

代码语言:javascript
复制
<?xml version="1.0"?>
<configuration>
        <startup useLegacyV2RuntimeActivationPolicy="true">
                <supportedRuntime version="v4.0.30319"/>
                <supportedRuntime version="v2.0.50727"/>
        </startup>
</configuration>

在此之后,任何针对更大.net版本的代码都将正常工作,例如,请查看以下内容:

代码语言:javascript
复制
Slytherin>> gc new.cs
using System.IO;

public static class testeo
{
        public static string joinP(string[] arr)
        {
                return Path.Combine(arr);
        }
}
Slytherin>> $arr = "uno", "dos", "tres", "cuatro", "cinco"
Slytherin>> Add-Type -Path .\new.cs
Slytherin>> [testeo]::joinP($arr)
uno\dos\tres\cuatro\cinco

该方法使用.NET4中定义的带有N个参数的Path.Combine。在.NET2中,Combine最多只能处理两个参数。我测试了使用匿名委托的其他示例,这是C#3的特征之一:

代码语言:javascript
复制
Slytherin>> $sharp = [IO.file]::readalltext((resolve-path new.cs))
Slytherin>> add-type  -typedef $sharp -Language CsharpVersion3
Slytherin>> gc .\new.cs
using System.IO;
using System;
using System.Linq;

namespace grantest
{

public static class testeo
{
        public static void joinP(string[] arr)
        {
                arr.ToList<string>().ForEach(x=>Console.WriteLine(x));

        }
}
}
Slytherin>> [grantest.testeo]::joinP($arr)
uno
dos
tres
cuatro
cinco
Slytherin>>

最后一个例子,如果你尝试下一段代码,它不会工作

代码语言:javascript
复制
Slytherin>> $sharp = [IO.file]::readalltext((resolve-path new.cs))
Slytherin>> $sharp
using System.IO;
using System;
using System.Linq;

namespace grantest
{

public static class testeo4
{
        public static string joinP(string[] arr)
        {
                arr.ToList<string>().ForEach(x=>Console.WriteLine(x));
                return Path.Combine(arr);
        }
}
}
Slytherin>> Add-Type -TypeDefinition $sharp
Add-Type : c:\Users\voodoomsr\AppData\Local\Temp\5ikkmpfn.0.cs(3) : The type or namesp
ace name 'Linq' does not exist in the namespace 'System' (are you missing an assembly
reference?)
c:\Users\voodoomsr\AppData\Local\Temp\5ikkmpfn.0.cs(2) : using System;
c:\Users\voodoomsr\AppData\Local\Temp\5ikkmpfn.0.cs(3) : >>> using System.Linq;
c:\Users\voodoomsr\AppData\Local\Temp\5ikkmpfn.0.cs(4) :
At line:1 char:9
+ Add-Type <<<<  -TypeDefinition $sharp
    + CategoryInfo          : InvalidData: (c:\Users\voodoo...bly reference?):Compile
   rError) [Add-Type], Exception
    + FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddType
   Command

其他人尝试指定language参数

代码语言:javascript
复制
Slytherin>> add-type -TypeDefinition $sharp -Language CSharpVersion3
Add-Type : c:\Users\voodoomsr\AppData\Local\Temp\v3r5whoc.0.cs(13) : No overload for m
ethod 'Combine' takes '1' arguments
c:\Users\voodoomsr\AppData\Local\Temp\v3r5whoc.0.cs(12) :         arr.ToList<string>()
.ForEach(x=>Console.WriteLine(x));
c:\Users\voodoomsr\AppData\Local\Temp\v3r5whoc.0.cs(13) : >>>         return Path.Comb
ine(arr);
c:\Users\voodoomsr\AppData\Local\Temp\v3r5whoc.0.cs(14) :     }
At line:1 char:9
+ add-type <<<<  -TypeDefinition $sharp -Language CSharpVersion3
    + CategoryInfo          : InvalidData: (c:\Users\voodoo...s '1' arguments:Compile
   rError) [Add-Type], Exception
    + FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddType
   Command

这是因为在使用.net4方法的情况下,很明显,如果您说语言是版本3,则会自动使用.net 3库。为了实现这一点,你需要添加语言作为引用的程序集,并忘记语言参数(可能值的枚举没有Csharpversion4),所以它将使用4版本,因为我们之前启用了它。

代码语言:javascript
复制
Slytherin>> Add-Type -TypeDefinition $sharp -ReferencedAssemblies System.core
Slytherin>> [grantest.testeo4]::joinP($arr)
uno
dos
tres
cuatro
cinco
uno\dos\tres\cuatro\cinco

祝你好运,并祝你编码愉快。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5475122

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档