首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何访问其他外接类型类型定义中的添加类型定义类型?

如何访问其他外接类型类型定义中的添加类型定义类型?
EN

Stack Overflow用户
提问于 2013-12-23 13:25:04
回答 2查看 9K关注 0票数 9

如何访问由Add-Type -TypeDefinition "..."在另一个Add-Type -TypeDefinition "..."中定义的类型

在下面的代码示例中,尽管名称空间相同,编译器仍然找不到UserCode类型。

代码语言:javascript
复制
Add-Type -TypeDefinition @"
namespace SampleCode {
    public struct UserCode {
         public string Name;
         public string Id;
    }
}
"@

#.... do something ....

Add-Type -TypeDefinition @"
namespace SampleCode {
    public struct UserInformation {
        public UserCode User;
        public string Note;
    }
}
"@
# => Error ... Add-Type : <temporary source path>(3) : The type or namespace name
# 'UserCode' could not be found (are you missing a using directive or an assembly
# reference?)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-12-23 18:23:53

为了从第二个.NET程序集引用第一个.NET程序集,需要将第一个.NET程序集持久化到磁盘。完成此操作后,可以使用.NET cmdlet的-ReferencedAssemblies参数引用第二个.NET程序集中的第一个Add-Type程序集。

重要:如果在两个不同的C#代码块中使用两个不同的名称空间,请确保在第二个代码块中适当地声明using语句。

下面是一个示例,向您展示如何:

  1. 将新的.NET程序集动态编译到磁盘
  2. 导入引用磁盘上另一个程序集的动态程序集。

第一部分-装配#1

必须将第一个.NET程序集输出到磁盘,以便我们可以引用它。因此,我们将使用-OutputAssembly-OutputType参数的Add-Type cmdlet来创建它。

代码语言:javascript
复制
$Csharp = @"
using System;
using System.Reflection;

namespace Widget {
    public class UserCode {
        public static int GetValue() {
            return 2;
        }
    }
}
"@

# Define the output path for the new .NET Assembly
$OutputAssembly = '{0}\Widget.dll' -f $env:USERPROFILE;

# Compile the code and output a new .NET assembly
Add-Type -TypeDefinition $Csharp -OutputAssembly $OutputAssembly -OutputType Library;

# Load the .NET Assembly 
[System.Reflection.Assembly]::LoadFile($OutputAssembly);

第2部分-组装#2

我们不必担心将第二个.NET程序集编译到磁盘上,因为我们没有从任何其他程序集中引用它。因此,我们可以简单地使用Add-Type cmdlet将其编译成一个临时的内存中的程序集。我们必须确保使用-ReferencedAssemblies参数引用我们在Part 1中编译的.NET程序集。

代码语言:javascript
复制
# Now that we have a compiled .NET Assembly, we need to write our new code
# that references it (make sure to include all appropriate C# "using" statements)

# Define the second set of C# code
$CsharpCode2 = @"
using Widget;

namespace NASA {
    public class Shuttle {
        public int Boosters;
        public Shuttle() {
            this.Boosters = UserCode.GetValue();
        }
    }
}
"@;

# Try to compile the new code, but wait, we get an error ...
# ... because this code is dependent on the code contained in the
# Widget assembly
$Assembly2 = Add-Type -TypeDefinition $CsharpCode2 -PassThru;

# We have to reference the .NET Assembly that defines the UserCode type
$Assembly2 = Add-Type -TypeDefinition $CsharpCode2 -ReferencedAssemblies $OutputAssembly -PassThru;

# Now we can successfully create the NASA.Shuttle object;
$Shuttle = New-Object -TypeName NASA.Shuttle;

# View the number of boosters the Shuttle instance has
Write-Host -Object $Shuttle.Boosters;
票数 12
EN

Stack Overflow用户

发布于 2013-12-23 17:24:58

这在动态程序集中是不可能的(默认情况下,如果不指定其他参数,则会生成这些程序集)。

您可以使用Add生成dll并在以后引用它们,如下所示:

代码语言:javascript
复制
Add-Type -OutputAssembly foo1.dll ...
Add-Type -ReferencedAssemblies foo1.dll ...

应该行得通。

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

https://stackoverflow.com/questions/20744525

复制
相关文章

相似问题

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